<a href="https://github.com/learnboost/mongoose"><img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a><html>
	<head>
		<title>Mongoose</title>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<style>body {
    margin: 0;
    padding: 0;
    font: 14px/1.5 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
    color: #252519;
}
a {
    color: #252519;
}
a:hover {
    text-decoration: underline;
    color: #19469D;
}
p {
    margin: 12px 0;
}
h1, h2, h3 {
    margin: 0;
    padding: 0;
}
table#source {
    width: 100%;
    border-collapse: collapse;
}
table#source td:first-child {
    padding: 30px 40px 30px 40px;
    vertical-align: top;
}
table#source td:first-child,
table#source td:first-child pre {
    width: 450px;
}
table#source td:last-child {
    padding: 30px 0 30px 40px;
    border-left: 1px solid #E5E5EE;
}
table#source tr {
    border-bottom: 1px solid #E5E5EE;
}
table#source tr.filename {
    padding-top: 40px;
    border-top: 1px solid #E5E5EE;
}
table#source tr.filename td {
    background: #F5F5FF;
}
table#source tr.filename td:first-child {
    text-transform: capitalize;
}
table#source tr.filename td:last-child {
    font-size: 12px;
}
table#source tr.filename h2 {
    margin: 0;
    padding: 0;
    cursor: pointer;
}
table#source tr.code h1,
table#source tr.code h2,
table#source tr.code h3 {
    margin-top: 30px;
    font-family: "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
    font-size: 18px;
}
table#source tr.code h2 {
    font-size: 16px;
}
table#source tr.code h3 {
    font-size: 14px;
}
table#source tr.code ul {
    margin: 15px 0 15px 35px;
    padding: 0;
}
table#source tr.code ul li {
    margin: 0;
    padding: 1px 0;
}
table#source tr.code ul li p {
    margin: 0;
    padding: 0;
}
table#source tr.code td.docs {
  padding-left: 75px;
}
table#source tr.code td:first-child pre {
    padding: 20px;
}
#ribbon {
    position: fixed;
    top: 0;
    right: 0;
}
code .string { color: #219161; }
code .regexp { color: #219161; }
code .keyword { color: #954121; }
code .number { color: #19469D; }
code .comment { color: #bbb; }
code .this { color: #19469D; }
</style>
		<script>
			$(function(){
        $('#source').on('dblclick', 'td', function (e) {
          var td = $(this)
            , tr = td.closest('tr')

          if (tr.is('.filename')) {
            tr.click();
          } else if (tr.is('.code')) {

            tr.prevAll('tr.filename:first').click();
          }
        });

				$('tr.filename').toggle(function(){
					$(this).nextUntil('.filename').fadeOut();
				}, function(){
					$(this).nextUntil('.filename').fadeIn();
				});
			});
		</script>
	</head>
	<body>
<table id="source"><tbody><tr><td><h1>Mongoose</h1><p>Expressive MongoDB for Node.JS</p></td><td></td></tr><tr class="filename"><td><h2 id="lib/collection.js"><a href="#">collection</a></h2></td><td>lib/collection.js</td></tr><tr class="code">
<td class="docs">
<p>Collection constructor</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  collection name</p></li><li><p><strong>param</strong>: <em>Collection</em>  connection object</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">Collection</span> (<span class="variable">name</span>, <span class="variable">conn</span>) {
  <span class="this">this</span>.<span class="variable">name</span> = <span class="variable">name</span>;
  <span class="this">this</span>.<span class="variable">conn</span> = <span class="variable">conn</span>;
  <span class="this">this</span>.<span class="variable">buffer</span> = <span class="variable">true</span>;
  <span class="this">this</span>.<span class="variable">queue</span> = [];
  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">conn</span>.<span class="variable">readyState</span> == <span class="number integer">1</span>) <span class="this">this</span>.<span class="variable">onOpen</span>();
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>The collection name</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Collection</span>.<span class="variable">prototype</span>.<span class="variable">name</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>The Connection instance</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Collection</span>.<span class="variable">prototype</span>.<span class="variable">conn</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">Collection</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/connection.js"><a href="#">connection</a></h2></td><td>lib/connection.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">url</span> = <span class="variable">require</span>(<span class="string">'url'</span>)
  , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>)
  , <span class="class">EventEmitter</span> = <span class="variable">utils</span>.<span class="class">EventEmitter</span>
  , <span class="variable">driver</span> = <span class="variable">global</span>.<span class="class">MONGOOSE_DRIVER_PATH</span> || <span class="string">'./drivers/node-mongodb-native'</span>
  , <span class="class">Model</span> = <span class="variable">require</span>(<span class="string">'./model'</span>)
  , <span class="class">Schema</span> = <span class="variable">require</span>(<span class="string">'./schema'</span>)
  , <span class="class">Collection</span>  = <span class="variable">require</span>(<span class="variable">driver</span> + <span class="string">'/collection'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connection constructor. For practical reasons, a Connection equals a Db</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Mongoose</em>  mongoose base</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">Connection</span> (<span class="variable">base</span>) {
  <span class="this">this</span>.<span class="variable">base</span> = <span class="variable">base</span>;
  <span class="this">this</span>.<span class="variable">collections</span> = {};
  <span class="this">this</span>.<span class="variable">models</span> = {};
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from EventEmitter.</p>

<p> </p>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">EventEmitter</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connection ready state:
 0 = Disconnected
 1 = Connected
 2 = Connecting
 3 = Disconnecting</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">readyState</span> = <span class="number integer">0</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A hash of the collections associated with this connection
 </p>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">collections</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>The mongodb.Db instance, set when the connection is opened</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">db</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Establishes the connection</p>

<p> <code>options</code> is a hash with the following optional properties:</p>

<p>  options.db      - passed to the connection db instance
  options.server  - passed to the connection server instance(s)
  options.replset - passed to the connection ReplSetServer instance
  options.user    - username for authentication
  options.pass    - password for authentication</p>

<p>  Notes:</p>

<p>  Mongoose forces the db option <code>forceServerObjectId</code> false and cannot
  be overridden.</p>

<p>  Mongoose defaults the server <code>auto_reconnect</code> options to true which
  can be overridden.</p>

<p>  See the node-mongodb-native driver instance for options that it
  understands.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  mongodb://uri</p></li><li><p><strong>return</strong>: <em>Connection</em>  self</p></li><li><p><strong>see</strong>: <em>https</em>
://github.com/christkv/node-mongodb-native</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">open</span> = <span class="keyword">function</span> (<span class="variable">host</span>, <span class="variable">database</span>, <span class="variable">port</span>, <span class="variable">options</span>, <span class="variable">callback</span>) {
  <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>
    , <span class="variable">uri</span>;

  <span class="keyword">if</span> (<span class="string">'string'</span> === <span class="keyword">typeof</span> <span class="variable">database</span>) {
    <span class="keyword">switch</span> (<span class="variable">arguments</span>.<span class="variable">length</span>) {
      <span class="keyword">case</span> <span class="number integer">2</span>:
        <span class="variable">port</span> = <span class="number integer">27017</span>;
      <span class="keyword">case</span> <span class="number integer">3</span>:
        <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">port</span>) {
          <span class="keyword">case</span> <span class="string">'function'</span>:
            <span class="variable">callback</span> = <span class="variable">port</span>, <span class="variable">port</span> = <span class="number integer">27017</span>;
            <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="string">'object'</span>:
            <span class="variable">options</span> = <span class="variable">port</span>, <span class="variable">port</span> = <span class="number integer">27017</span>;
            <span class="keyword">break</span>;
        }
        <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="number integer">4</span>:
        <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">options</span>)
          <span class="variable">callback</span> = <span class="variable">options</span>, <span class="variable">options</span> = {};
    }
  } <span class="keyword">else</span> {
    <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">database</span>) {
      <span class="keyword">case</span> <span class="string">'function'</span>:
        <span class="variable">callback</span> = <span class="variable">database</span>, <span class="variable">database</span> = <span class="variable">undefined</span>;
        <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="string">'object'</span>:
        <span class="variable">options</span> = <span class="variable">database</span>;
        <span class="variable">database</span> = <span class="variable">undefined</span>;
        <span class="variable">callback</span> = <span class="variable">port</span>;
        <span class="keyword">break</span>;
    }

    <span class="variable">uri</span> = <span class="variable">url</span>.<span class="variable">parse</span>(<span class="variable">host</span>);
    <span class="variable">host</span> = <span class="variable">uri</span>.<span class="variable">hostname</span>;
    <span class="variable">port</span> = <span class="variable">uri</span>.<span class="variable">port</span> || <span class="number integer">27017</span>;
    <span class="variable">database</span> = <span class="variable">uri</span>.<span class="variable">pathname</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">uri</span>.<span class="variable">pathname</span>.<span class="variable">replace</span>(<span class="regexp">/\/</span>/<span class="variable">g</span>, <span class="string">''</span>);
  }

  <span class="variable">callback</span> = <span class="variable">callback</span> || <span class="variable">noop</span>;
  <span class="this">this</span>.<span class="variable">options</span> = <span class="this">this</span>.<span class="variable">defaultOptions</span>(<span class="variable">options</span>);

  <span class="comment">// make sure we can open</span>
  <span class="keyword">if</span> (<span class="number integer">0</span> !== <span class="this">this</span>.<span class="variable">readyState</span>) {
    <span class="keyword">var</span> <span class="variable">err</span> = <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'Trying to open unclosed connection.'</span>);
    <span class="variable">err</span>.<span class="variable">state</span> = <span class="this">this</span>.<span class="variable">readyState</span>;
    <span class="variable">callback</span>(<span class="variable">err</span>);
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="keyword">if</span> (!<span class="variable">host</span>) {
    <span class="variable">callback</span>(<span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'Missing connection hostname.'</span>));
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="keyword">if</span> (!<span class="variable">database</span>) {
    <span class="variable">callback</span>(<span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'Missing connection database.'</span>));
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="comment">// handle authentication</span>
  <span class="keyword">if</span> (<span class="variable">uri</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">uri</span>.<span class="variable">auth</span>) {
    <span class="keyword">var</span> <span class="variable">auth</span> = <span class="variable">uri</span>.<span class="variable">auth</span>.<span class="variable">split</span>(<span class="string">':'</span>);
    <span class="this">this</span>.<span class="variable">user</span> = <span class="variable">auth</span>[<span class="number integer">0</span>];
    <span class="this">this</span>.<span class="variable">pass</span> = <span class="variable">auth</span>[<span class="number integer">1</span>];

  <span class="comment">// Check hostname for user/pass</span>
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="regexp">/@/</span>.<span class="variable">test</span>(<span class="variable">host</span>) &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="regexp">/:/</span>.<span class="variable">test</span>(<span class="variable">host</span>.<span class="variable">split</span>(<span class="string">'@'</span>)[<span class="number integer">0</span>])) {
    <span class="variable">host</span> = <span class="variable">host</span>.<span class="variable">split</span>(<span class="string">'@'</span>);
    <span class="keyword">var</span> <span class="variable">auth</span> = <span class="variable">host</span>.<span class="variable">shift</span>().<span class="variable">split</span>(<span class="string">':'</span>);
    <span class="variable">host</span> = <span class="variable">host</span>.<span class="variable">pop</span>();
    <span class="this">this</span>.<span class="variable">user</span> = <span class="variable">auth</span>[<span class="number integer">0</span>];
    <span class="this">this</span>.<span class="variable">pass</span> = <span class="variable">auth</span>[<span class="number integer">1</span>];

  <span class="comment">// user/pass options</span>
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">options</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">options</span>.<span class="variable">user</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">options</span>.<span class="variable">pass</span>) {
    <span class="this">this</span>.<span class="variable">user</span> = <span class="variable">options</span>.<span class="variable">user</span>;
    <span class="this">this</span>.<span class="variable">pass</span> = <span class="variable">options</span>.<span class="variable">pass</span>;

  } <span class="keyword">else</span> {
    <span class="this">this</span>.<span class="variable">user</span> = <span class="this">this</span>.<span class="variable">pass</span> = <span class="variable">undefined</span>;
  }

  <span class="this">this</span>.<span class="variable">name</span> = <span class="variable">database</span>;
  <span class="this">this</span>.<span class="variable">host</span> = <span class="variable">host</span>;
  <span class="this">this</span>.<span class="variable">port</span> = <span class="variable">port</span>;

  <span class="comment">// signal connecting</span>
  <span class="this">this</span>.<span class="variable">readyState</span> = <span class="number integer">2</span>;
  <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'opening'</span>);

  <span class="comment">// open connection</span>
  <span class="this">this</span>.<span class="variable">doOpen</span>(<span class="keyword">function</span> (<span class="variable">err</span>) {
    <span class="keyword">if</span> (<span class="variable">err</span>) {
      <span class="variable">self</span>.<span class="variable">readyState</span> = <span class="number integer">0</span>;
      <span class="keyword">if</span> (<span class="variable">self</span>.<span class="variable">_events</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">self</span>.<span class="variable">_events</span>.<span class="variable">error</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>;
         (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">self</span>.<span class="variable">_events</span>.<span class="variable">error</span> || <span class="variable">self</span>.<span class="variable">_events</span>.<span class="variable">error</span>.<span class="variable">length</span>)) {
        <span class="variable">self</span>.<span class="variable">emit</span>(&<span class="variable">quot</span>;<span class="variable">error</span>&<span class="variable">quot</span>;, <span class="variable">err</span>);
      }
    } <span class="keyword">else</span> {
      <span class="variable">self</span>.<span class="variable">onOpen</span>();
    }

    <span class="variable">callback</span>(<span class="variable">err</span> || <span class="keyword">null</span>);
  });

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connects to a replica set.</p>

<p>Supply a comma-separted list of mongodb:// URIs. You only need to specify
the database name and/or auth to one of them.</p>

<p>The options parameter is passed to the low level connection. See the
node-mongodb-native driver instance for detail.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  comma-separated mongodb:// URIs</p></li><li><p><strong>param</strong>: <em>String</em>  optional database name</p></li><li><p><strong>param</strong>: <em>Object</em>  optional options</p></li><li><p><strong>param</strong>: <em>Function</em>  optional callback</p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">openSet</span> = <span class="keyword">function</span> (<span class="variable">uris</span>, <span class="variable">database</span>, <span class="variable">options</span>, <span class="variable">callback</span>) {
  <span class="keyword">var</span> <span class="variable">uris</span> = <span class="variable">uris</span>.<span class="variable">split</span>(<span class="string">','</span>)
    , <span class="variable">self</span> = <span class="this">this</span>;

  <span class="keyword">switch</span> (<span class="variable">arguments</span>.<span class="variable">length</span>) {
    <span class="keyword">case</span> <span class="number integer">3</span>:
      <span class="this">this</span>.<span class="variable">name</span> = <span class="variable">database</span>;
      <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">options</span>) <span class="variable">callback</span> = <span class="variable">options</span>, <span class="variable">options</span> = {};
      <span class="keyword">break</span>;
    <span class="keyword">case</span> <span class="number integer">2</span>:
      <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">database</span>) {
        <span class="keyword">case</span> <span class="string">'string'</span>:
          <span class="this">this</span>.<span class="variable">name</span> = <span class="variable">database</span>;
        <span class="keyword">case</span> <span class="string">'function'</span>:
          <span class="variable">callback</span> = <span class="variable">database</span>, <span class="variable">database</span> = <span class="keyword">null</span>;
          <span class="keyword">break</span>;
        <span class="keyword">case</span> <span class="string">'object'</span>:
          <span class="variable">options</span> = <span class="variable">database</span>, <span class="variable">database</span> = <span class="keyword">null</span>;
          <span class="keyword">break</span>;
      }
  }

  <span class="this">this</span>.<span class="variable">options</span> = <span class="variable">options</span> = <span class="this">this</span>.<span class="variable">defaultOptions</span>(<span class="variable">options</span>);
  <span class="variable">callback</span> = <span class="variable">callback</span> || <span class="variable">noop</span>;

  <span class="keyword">if</span> (<span class="variable">uris</span>.<span class="variable">length</span> &<span class="variable">lt</span>; <span class="number integer">2</span>) {
    <span class="variable">callback</span>(<span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'Please provide comma-separated URIs'</span>));
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="this">this</span>.<span class="variable">host</span> = [];
  <span class="this">this</span>.<span class="variable">port</span> = [];

  <span class="variable">uris</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">uri</span>) {
    <span class="keyword">var</span> <span class="variable">uri</span> = <span class="variable">url</span>.<span class="variable">parse</span>(<span class="variable">uri</span>);

    <span class="variable">self</span>.<span class="variable">host</span>.<span class="variable">push</span>(<span class="variable">uri</span>.<span class="variable">hostname</span>);
    <span class="variable">self</span>.<span class="variable">port</span>.<span class="variable">push</span>(<span class="variable">uri</span>.<span class="variable">port</span> || <span class="number integer">27017</span>);

    <span class="keyword">if</span> (!<span class="variable">self</span>.<span class="variable">name</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">uri</span>.<span class="variable">pathname</span>.<span class="variable">replace</span>(<span class="regexp">/\/</span>/<span class="variable">g</span>, <span class="string">''</span>))
      <span class="variable">self</span>.<span class="variable">name</span> = <span class="variable">uri</span>.<span class="variable">pathname</span>.<span class="variable">replace</span>(<span class="regexp">/\/</span>/<span class="variable">g</span>, <span class="string">''</span>);

    <span class="keyword">if</span> (!<span class="variable">self</span>.<span class="variable">user</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">uri</span>.<span class="variable">auth</span>) {
      <span class="keyword">var</span> <span class="variable">auth</span> = <span class="variable">uri</span>.<span class="variable">auth</span>.<span class="variable">split</span>(<span class="string">':'</span>);
      <span class="variable">self</span>.<span class="variable">user</span> = <span class="variable">auth</span>[<span class="number integer">0</span>];
      <span class="variable">self</span>.<span class="variable">pass</span> = <span class="variable">auth</span>[<span class="number integer">1</span>];
    }
  });

  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">name</span>) {
    <span class="variable">callback</span>(<span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'No database name provided for replica set'</span>));
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="this">this</span>.<span class="variable">readyState</span> = <span class="number integer">2</span>;
  <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'opening'</span>);

  <span class="comment">// open connection</span>
  <span class="this">this</span>.<span class="variable">doOpenSet</span>(<span class="keyword">function</span> (<span class="variable">err</span>) {
    <span class="keyword">if</span> (<span class="variable">err</span>) {
      <span class="keyword">if</span> (<span class="variable">self</span>.<span class="variable">_events</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">self</span>.<span class="variable">_events</span>.<span class="variable">error</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">self</span>.<span class="variable">_events</span>.<span class="variable">error</span>.<span class="variable">length</span>) {
        <span class="variable">self</span>.<span class="variable">emit</span>(&<span class="variable">quot</span>;<span class="variable">error</span>&<span class="variable">quot</span>;, <span class="variable">err</span>);
      }
      <span class="variable">self</span>.<span class="variable">readyState</span> = <span class="number integer">0</span>;
    } <span class="keyword">else</span> {
      <span class="variable">self</span>.<span class="variable">onOpen</span>();
    }

    <span class="variable">callback</span>(<span class="variable">err</span> || <span class="keyword">null</span>);
  });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Closes the connection</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  optional callback</p></li><li><p><strong>return</strong>: <em>Connection</em>  self</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">close</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
  <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>
    , <span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span>(){};

  <span class="keyword">switch</span> (<span class="this">this</span>.<span class="variable">readyState</span>){
    <span class="keyword">case</span> <span class="number integer">0</span>: <span class="comment">// disconnected</span>
      <span class="variable">callback</span>(<span class="keyword">null</span>);
      <span class="keyword">break</span>;

    <span class="keyword">case</span> <span class="number integer">1</span>: <span class="comment">// connected</span>
      <span class="this">this</span>.<span class="variable">readyState</span> = <span class="number integer">3</span>;
      <span class="this">this</span>.<span class="variable">doClose</span>(<span class="keyword">function</span>(<span class="variable">err</span>){
        <span class="keyword">if</span> (<span class="variable">err</span>){
          <span class="variable">callback</span>(<span class="variable">err</span>);
        } <span class="keyword">else</span> {
          <span class="variable">self</span>.<span class="variable">onClose</span>();
          <span class="variable">callback</span>(<span class="keyword">null</span>);
        }
      });
      <span class="keyword">break</span>;

    <span class="keyword">case</span> <span class="number integer">2</span>: <span class="comment">// connecting</span>
      <span class="this">this</span>.<span class="variable">once</span>(<span class="string">'open'</span>, <span class="keyword">function</span>(){
        <span class="variable">self</span>.<span class="variable">close</span>(<span class="variable">callback</span>);
      });
      <span class="keyword">break</span>;

    <span class="keyword">case</span> <span class="number integer">3</span>: <span class="comment">// disconnecting</span>
      <span class="this">this</span>.<span class="variable">once</span>(<span class="string">'close'</span>, <span class="keyword">function</span> () {
        <span class="variable">callback</span>(<span class="keyword">null</span>);
      });
      <span class="keyword">break</span>;
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Retrieves a collection, creating it if not cached.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  collection name</p></li><li><p><strong>return</strong>: <em>Collection</em>  collection instance</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">collection</span> = <span class="keyword">function</span> (<span class="variable">name</span>) {
  <span class="keyword">if</span> (!(<span class="variable">name</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">collections</span>))
    <span class="this">this</span>.<span class="variable">collections</span>[<span class="variable">name</span>] = <span class="keyword">new</span> <span class="class">Collection</span>(<span class="variable">name</span>, <span class="this">this</span>);
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">collections</span>[<span class="variable">name</span>];
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Defines a model or retrieves it</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  model name</p></li><li><p><strong>param</strong>: <em>Schema</em>  schema object</p></li><li><p><strong>param</strong>: <em>String</em>  collection name (optional, induced from model name)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">model</span> = <span class="keyword">function</span> (<span class="variable">name</span>, <span class="variable">schema</span>, <span class="variable">collection</span>) {
  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">models</span>[<span class="variable">name</span>]) {
    <span class="keyword">var</span> <span class="variable">model</span> = <span class="this">this</span>.<span class="variable">base</span>.<span class="variable">model</span>(<span class="variable">name</span>, <span class="variable">schema</span>, <span class="variable">collection</span>, <span class="variable">true</span>)
      , <span class="class">Model</span>

    <span class="keyword">if</span> (<span class="this">this</span> != <span class="variable">model</span>.<span class="variable">prototype</span>.<span class="variable">db</span>) {
      <span class="comment">// subclass model using this connection and collection name</span>
      <span class="class">Model</span> = <span class="keyword">function</span> <span class="class">Model</span> () {
        <span class="variable">model</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
      };

      <span class="class">Model</span>.<span class="variable">__proto__</span> = <span class="variable">model</span>;
      <span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="variable">model</span>.<span class="variable">prototype</span>;
      <span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">db</span> = <span class="this">this</span>;

      <span class="comment">// collection name discovery</span>
      <span class="keyword">if</span> (<span class="string">'string'</span> === <span class="keyword">typeof</span> <span class="variable">schema</span>) {
        <span class="variable">collection</span> = <span class="variable">schema</span>;
      }

      <span class="keyword">if</span> (!<span class="variable">collection</span>) {
        <span class="variable">collection</span> = <span class="variable">model</span>.<span class="variable">prototype</span>.<span class="variable">schema</span>.<span class="variable">set</span>(<span class="string">'collection'</span>) || <span class="variable">utils</span>.<span class="variable">toCollectionName</span>(<span class="variable">name</span>);
      }

      <span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">collection</span> = <span class="this">this</span>.<span class="variable">collection</span>(<span class="variable">collection</span>);
      <span class="class">Model</span>.<span class="variable">init</span>();
    }

    <span class="this">this</span>.<span class="variable">models</span>[<span class="variable">name</span>] = <span class="class">Model</span> || <span class="variable">model</span>;
  }

  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">models</span>[<span class="variable">name</span>];
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Set profiling level.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Int | String</em>  level - Either off (0), slow (1), or all (2)</p></li><li><p><strong>param</strong>: <em>Int</em>  [ms]  If profiling <code>level</code> is set to 1, this determines</p><pre><code>           the threshold in milliseconds above which queries
           will be logged. Defaults to 100. (optional)</code></pre></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Connection</span>.<span class="variable">prototype</span>.<span class="variable">setProfiling</span> = <span class="keyword">function</span> (<span class="variable">level</span>, <span class="variable">ms</span>, <span class="variable">callback</span>) {
  <span class="keyword">if</span> (<span class="number integer">1</span> !== <span class="this">this</span>.<span class="variable">readyState</span>) {
    <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">on</span>(<span class="string">'open'</span>, <span class="this">this</span>.<span class="variable">setProfiling</span>.<span class="variable">bind</span>(<span class="this">this</span>, <span class="variable">level</span>, <span class="variable">ms</span>, <span class="variable">callback</span>));
  }

  <span class="keyword">if</span> (!<span class="variable">callback</span>) <span class="variable">callback</span> = <span class="variable">ms</span>, <span class="variable">ms</span> = <span class="number integer">100</span>;

  <span class="keyword">var</span> <span class="variable">cmd</span> = {};

  <span class="keyword">switch</span> (<span class="variable">level</span>) {
    <span class="keyword">case</span> <span class="number integer">0</span>:
    <span class="keyword">case</span> <span class="string">'off'</span>:
      <span class="variable">cmd</span>.<span class="variable">profile</span> = <span class="number integer">0</span>;
      <span class="keyword">break</span>;
    <span class="keyword">case</span> <span class="number integer">1</span>:
    <span class="keyword">case</span> <span class="string">'slow'</span>:
      <span class="variable">cmd</span>.<span class="variable">profile</span> = <span class="number integer">1</span>;
      <span class="keyword">if</span> (<span class="string">'number'</span> !== <span class="keyword">typeof</span> <span class="variable">ms</span>) {
        <span class="variable">ms</span> = <span class="variable">parseInt</span>(<span class="variable">ms</span>, <span class="number integer">10</span>);
        <span class="keyword">if</span> (<span class="variable">isNaN</span>(<span class="variable">ms</span>)) <span class="variable">ms</span> = <span class="number integer">100</span>;
      }
      <span class="variable">cmd</span>.<span class="variable">slowms</span> = <span class="variable">ms</span>;
      <span class="keyword">break</span>;
    <span class="keyword">case</span> <span class="number integer">2</span>:
    <span class="keyword">case</span> <span class="string">'all'</span>:
      <span class="variable">cmd</span>.<span class="variable">profile</span> = <span class="number integer">2</span>;
      <span class="keyword">break</span>;
    <span class="keyword">default</span>:
      <span class="keyword">return</span> <span class="variable">callback</span>(<span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'Invalid profiling level: '</span>+ <span class="variable">level</span>));
  }

  <span class="this">this</span>.<span class="variable">db</span>.<span class="variable">executeDbCommand</span>(<span class="variable">cmd</span>, <span class="keyword">function</span> (<span class="variable">err</span>, <span class="variable">resp</span>) {
    <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>);

    <span class="keyword">var</span> <span class="variable">doc</span> = <span class="variable">resp</span>.<span class="variable">documents</span>[<span class="number integer">0</span>];

    <span class="variable">err</span> = <span class="number integer">1</span> === <span class="variable">doc</span>.<span class="variable">ok</span>
      ? <span class="keyword">null</span>
      : <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'Could not set profiling level to: '</span>+ <span class="variable">level</span>)

    <span class="variable">callback</span>(<span class="variable">err</span>, <span class="variable">doc</span>);
  });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Noop.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">noop</span> () {}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">Connection</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/document.js"><a href="#">document</a></h2></td><td>lib/document.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">EventEmitter</span> = <span class="variable">require</span>(<span class="string">'events'</span>).<span class="class">EventEmitter</span>
  , <span class="class">MongooseError</span> = <span class="variable">require</span>(<span class="string">'./error'</span>)
  , <span class="class">MixedSchema</span> = <span class="variable">require</span>(<span class="string">'./schema/mixed'</span>)
  , <span class="class">Schema</span> = <span class="variable">require</span>(<span class="string">'./schema'</span>)
  , <span class="class">ValidatorError</span> = <span class="variable">require</span>(<span class="string">'./schematype'</span>).<span class="class">ValidatorError</span>
  , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>)
  , <span class="variable">clone</span> = <span class="variable">utils</span>.<span class="variable">clone</span>
  , <span class="variable">isMongooseObject</span> = <span class="variable">utils</span>.<span class="variable">isMongooseObject</span>
  , <span class="variable">inspect</span> = <span class="variable">require</span>(<span class="string">'util'</span>).<span class="variable">inspect</span>
  , <span class="class">StateMachine</span> = <span class="variable">require</span>(<span class="string">'./statemachine'</span>)
  , <span class="class">ActiveRoster</span> = <span class="class">StateMachine</span>.<span class="variable">ctor</span>(<span class="string">'require'</span>, <span class="string">'modify'</span>, <span class="string">'init'</span>, <span class="string">'default'</span>)
  , <span class="variable">deepEqual</span> = <span class="variable">utils</span>.<span class="variable">deepEqual</span>
  , <span class="variable">hooks</span> = <span class="variable">require</span>(<span class="string">'hooks'</span>)
  , <span class="class">DocumentArray</span></code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from EventEmitter.
 </p>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">EventEmitter</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Base Mongoose instance for the model. Set by the Mongoose instance upon
pre-compilation.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">base</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Document schema as a nested structure.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">schema</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Whether the document is new.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">isNew</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Validation errors.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">errors</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets a path, or many paths</p>

<h2>Examples</h2>

<pre><code>// path, value
doc.set(path, value)

// object
doc.set({
    path  : value
  , path2 : {
       path  : value
    }
}</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String | Object</em>  key path, or object</p></li><li><p><strong>param</strong>: <em>Object</em>  value, or undefined or a prefix if first parameter is an object</p></li><li><p><strong>para</strong>: <em>m</em>
@optional {Schema|String|...} specify a type if this is an on-the-fly attribute</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>, <span class="variable">type</span>) {
  <span class="keyword">var</span> <span class="variable">constructing</span> = <span class="variable">true</span> === <span class="variable">type</span>
    , <span class="variable">adhoc</span> = <span class="variable">type</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">true</span> !== <span class="variable">type</span>
    , <span class="variable">adhocs</span>

  <span class="keyword">if</span> (<span class="variable">adhoc</span>) {
    <span class="variable">adhocs</span> = <span class="this">this</span>.<span class="variable">_adhocPaths</span> || (<span class="this">this</span>.<span class="variable">_adhocPaths</span> = {});
    <span class="variable">adhocs</span>[<span class="variable">path</span>] = <span class="class">Schema</span>.<span class="variable">interpretAsType</span>(<span class="variable">path</span>, <span class="variable">type</span>);
  }

  <span class="keyword">if</span> (<span class="string">'string'</span> !== <span class="keyword">typeof</span> <span class="variable">path</span>) {
    <span class="comment">// new Document({ key: val })</span>

    <span class="keyword">if</span> (<span class="keyword">null</span> === <span class="variable">path</span> || <span class="variable">undefined</span> === <span class="variable">path</span>) {
      <span class="keyword">var</span> <span class="variable">_</span> = <span class="variable">path</span>;
      <span class="variable">path</span> = <span class="variable">val</span>;
      <span class="variable">val</span> = <span class="variable">_</span>;

    } <span class="keyword">else</span> {
      <span class="keyword">var</span> <span class="variable">prefix</span> = <span class="variable">val</span>
        ? <span class="variable">val</span> + <span class="string">'.'</span>
        : <span class="string">''</span>;

      <span class="keyword">if</span> (<span class="variable">path</span> <span class="variable">instanceof</span> <span class="class">Document</span>) <span class="variable">path</span> = <span class="variable">path</span>.<span class="variable">_doc</span>;

      <span class="keyword">var</span> <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">path</span>)
        , <span class="variable">i</span> = <span class="variable">keys</span>.<span class="variable">length</span>
        , <span class="variable">pathtype</span>
        , <span class="variable">key</span>

      <span class="keyword">while</span> (<span class="variable">i</span>--) {
        <span class="variable">key</span> = <span class="variable">keys</span>[<span class="variable">i</span>];
        <span class="keyword">if</span> (<span class="keyword">null</span> != <span class="variable">path</span>[<span class="variable">key</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'Object'</span> === <span class="variable">path</span>[<span class="variable">key</span>].<span class="variable">constructor</span>.<span class="variable">name</span>
          &<span class="variable">amp</span>;&<span class="variable">amp</span>; !(<span class="this">this</span>.<span class="variable">_path</span>(<span class="variable">prefix</span> + <span class="variable">key</span>) <span class="variable">instanceof</span> <span class="class">MixedSchema</span>)) {
          <span class="this">this</span>.<span class="variable">set</span>(<span class="variable">path</span>[<span class="variable">key</span>], <span class="variable">prefix</span> + <span class="variable">key</span>, <span class="variable">constructing</span>);
        } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">_strictMode</span>) {
          <span class="variable">pathtype</span> = <span class="this">this</span>.<span class="variable">schema</span>.<span class="variable">pathType</span>(<span class="variable">prefix</span> + <span class="variable">key</span>);
          <span class="keyword">if</span> (<span class="string">'real'</span> === <span class="variable">pathtype</span> || <span class="string">'virtual'</span> === <span class="variable">pathtype</span>) {
            <span class="this">this</span>.<span class="variable">set</span>(<span class="variable">prefix</span> + <span class="variable">key</span>, <span class="variable">path</span>[<span class="variable">key</span>], <span class="variable">constructing</span>);
          }
        } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">undefined</span> !== <span class="variable">path</span>[<span class="variable">key</span>]) {
          <span class="this">this</span>.<span class="variable">set</span>(<span class="variable">prefix</span> + <span class="variable">key</span>, <span class="variable">path</span>[<span class="variable">key</span>], <span class="variable">constructing</span>);
        }
      }

      <span class="keyword">return</span> <span class="this">this</span>;
    }
  }

  <span class="comment">// ensure _strict is honored for obj props</span>
  <span class="comment">// docschema = new Schema({ path: { nest: 'string' }})</span>
  <span class="comment">// doc.set('path', obj);</span>
  <span class="keyword">var</span> <span class="variable">pathType</span> = <span class="this">this</span>.<span class="variable">schema</span>.<span class="variable">pathType</span>(<span class="variable">path</span>);
  <span class="keyword">if</span> (<span class="string">'nested'</span> == <span class="variable">pathType</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">val</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'Object'</span> == <span class="variable">val</span>.<span class="variable">constructor</span>.<span class="variable">name</span>) {
    <span class="this">this</span>.<span class="variable">set</span>(<span class="variable">val</span>, <span class="variable">path</span>, <span class="variable">constructing</span>);
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="keyword">var</span> <span class="variable">schema</span>;
  <span class="keyword">if</span> (<span class="string">'adhocOrUndefined'</span> == <span class="variable">pathType</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>.<span class="variable">_strictMode</span>) {
    <span class="keyword">return</span> <span class="this">this</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'virtual'</span> == <span class="variable">pathType</span>) {
    <span class="variable">schema</span> = <span class="this">this</span>.<span class="variable">schema</span>.<span class="variable">virtualpath</span>(<span class="variable">path</span>);
    <span class="variable">schema</span>.<span class="variable">applySetters</span>(<span class="variable">val</span>, <span class="this">this</span>);
    <span class="keyword">return</span> <span class="this">this</span>;
  } <span class="keyword">else</span> {
    <span class="variable">schema</span> = <span class="this">this</span>.<span class="variable">_path</span>(<span class="variable">path</span>);
  }

  <span class="keyword">var</span> <span class="variable">parts</span> = <span class="variable">path</span>.<span class="variable">split</span>(<span class="string">'.'</span>)
    , <span class="variable">obj</span> = <span class="this">this</span>.<span class="variable">_doc</span>
    , <span class="variable">self</span> = <span class="this">this</span>
    , <span class="variable">pathToMark</span>
    , <span class="variable">subpaths</span>
    , <span class="variable">subpath</span>

  <span class="comment">// When using the $set operator the path to the field must already exist.</span>
  <span class="comment">// Else mongodb throws: &quot;LEFT_SUBFIELD only supports Object&quot;</span>

  <span class="keyword">if</span> (<span class="variable">parts</span>.<span class="variable">length</span> &<span class="variable">lt</span>;= <span class="number integer">1</span>) {
    <span class="variable">pathToMark</span> = <span class="variable">path</span>;
  } <span class="keyword">else</span> {
    <span class="variable">subpaths</span> = <span class="variable">parts</span>.<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">part</span>, <span class="variable">i</span>) {
      <span class="keyword">return</span> <span class="variable">parts</span>.<span class="variable">slice</span>(<span class="number integer">0</span>, <span class="variable">i</span>).<span class="variable">concat</span>(<span class="variable">part</span>).<span class="variable">join</span>(<span class="string">'.'</span>);
    });

    <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="variable">subpaths</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
      <span class="variable">subpath</span> = <span class="variable">subpaths</span>[<span class="variable">i</span>];
      <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">isDirectModified</span>(<span class="variable">subpath</span>) <span class="comment">// earlier prefixes that are already</span>
                                         <span class="comment">// marked as dirty have precedence</span>
          || <span class="this">this</span>.<span class="variable">get</span>(<span class="variable">subpath</span>) === <span class="keyword">null</span>) {
        <span class="variable">pathToMark</span> = <span class="variable">subpath</span>;
        <span class="keyword">break</span>;
      }
    }

    <span class="keyword">if</span> (!<span class="variable">pathToMark</span>) <span class="variable">pathToMark</span> = <span class="variable">path</span>;
  }

  <span class="keyword">if</span> ((!<span class="variable">schema</span> || <span class="keyword">null</span> === <span class="variable">val</span> || <span class="variable">undefined</span> === <span class="variable">val</span>) ||
    <span class="this">this</span>.<span class="keyword">try</span>(<span class="keyword">function</span>(){
      <span class="comment">// if this doc is being constructed we should not</span>
      <span class="comment">// trigger getters.</span>
      <span class="keyword">var</span> <span class="variable">cur</span> = <span class="variable">constructing</span> ? <span class="variable">undefined</span> : <span class="variable">self</span>.<span class="variable">get</span>(<span class="variable">path</span>);
      <span class="keyword">var</span> <span class="variable">casted</span> = <span class="variable">schema</span>.<span class="variable">cast</span>(<span class="variable">val</span>, <span class="variable">self</span>, <span class="variable">false</span>, <span class="variable">cur</span>);
      <span class="variable">val</span> = <span class="variable">schema</span>.<span class="variable">applySetters</span>(<span class="variable">casted</span>, <span class="variable">self</span>);
    })) {

    <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">isNew</span>) {
      <span class="this">this</span>.<span class="variable">markModified</span>(<span class="variable">pathToMark</span>);
    } <span class="keyword">else</span> {
      <span class="keyword">var</span> <span class="variable">priorVal</span> = <span class="this">this</span>.<span class="variable">get</span>(<span class="variable">path</span>);

      <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">isDirectModified</span>(<span class="variable">pathToMark</span>)) {
        <span class="keyword">if</span> (<span class="variable">undefined</span> === <span class="variable">val</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="this">this</span>.<span class="variable">isSelected</span>(<span class="variable">path</span>)) {
          <span class="comment">// special case:</span>
          <span class="comment">// when a path is not selected in a query its initial</span>
          <span class="comment">// value will be undefined.</span>
          <span class="this">this</span>.<span class="variable">markModified</span>(<span class="variable">pathToMark</span>);
        } <span class="keyword">else</span> <span class="keyword">if</span> (!<span class="variable">deepEqual</span>(<span class="variable">val</span>, <span class="variable">priorVal</span>)) {
          <span class="this">this</span>.<span class="variable">markModified</span>(<span class="variable">pathToMark</span>);
        } <span class="keyword">else</span> <span class="keyword">if</span> (!<span class="variable">constructing</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>;
                   <span class="keyword">null</span> != <span class="variable">val</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>;
                   <span class="variable">path</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">states</span>.<span class="keyword">default</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>;
                   <span class="variable">deepEqual</span>(<span class="variable">val</span>, <span class="variable">schema</span>.<span class="variable">getDefault</span>(<span class="this">this</span>, <span class="variable">constructing</span>))) {
          <span class="comment">// special case:</span>
          <span class="comment">// a path with a default was $unset on the server</span>
          <span class="comment">// and the user is setting it to the same value again</span>
          <span class="this">this</span>.<span class="variable">markModified</span>(<span class="variable">pathToMark</span>);
        }
      }
    }

    <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="variable">parts</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
      <span class="keyword">var</span> <span class="variable">next</span> = <span class="variable">i</span> + <span class="number integer">1</span>
        , <span class="variable">last</span> = <span class="variable">next</span> === <span class="variable">l</span>;

      <span class="keyword">if</span> (<span class="variable">last</span>) {
        <span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]] = <span class="variable">val</span>;
      } <span class="keyword">else</span> {
        <span class="keyword">if</span> (<span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'Object'</span> === <span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]].<span class="variable">constructor</span>.<span class="variable">name</span>) {
          <span class="variable">obj</span> = <span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]];
        } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]])) {
          <span class="variable">obj</span> = <span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]];
        } <span class="keyword">else</span> {
          <span class="variable">obj</span> = <span class="variable">obj</span>[<span class="variable">parts</span>[<span class="variable">i</span>]] = {};
        }
      }
    }
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Triggers casting on a specific path</p>

<ul><li><strong>tod</strong>: <em>o</em></li><li><p>deprecate? not used anywhere
## </p></li><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">doCast</span> = <span class="keyword">function</span> (<span class="variable">path</span>) {
  <span class="keyword">var</span> <span class="variable">schema</span> = <span class="this">this</span>.<span class="variable">schema</span>.<span class="variable">path</span>(<span class="variable">path</span>);
  <span class="keyword">if</span> (<span class="variable">schema</span>)
    <span class="this">this</span>.<span class="variable">setValue</span>(<span class="variable">path</span>, <span class="this">this</span>.<span class="variable">getValue</span>(<span class="variable">path</span>));
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Gets a path</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  key path</p></li><li><p><strong>para</strong>: <em>m</em>
@optional {Schema|String|...} specify a type if this is an on-the-fly attribute</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">type</span>) {
  <span class="keyword">var</span> <span class="variable">adhocs</span>;
  <span class="keyword">if</span> (<span class="variable">type</span>) {
    <span class="variable">adhocs</span> = <span class="this">this</span>.<span class="variable">_adhocPaths</span> || (<span class="this">this</span>.<span class="variable">_adhocPaths</span> = {});
    <span class="variable">adhocs</span>[<span class="variable">path</span>] = <span class="class">Schema</span>.<span class="variable">interpretAsType</span>(<span class="variable">path</span>, <span class="variable">type</span>);
  }

  <span class="keyword">var</span> <span class="variable">schema</span> = <span class="this">this</span>.<span class="variable">_path</span>(<span class="variable">path</span>) || <span class="this">this</span>.<span class="variable">schema</span>.<span class="variable">virtualpath</span>(<span class="variable">path</span>)
    , <span class="variable">pieces</span> = <span class="variable">path</span>.<span class="variable">split</span>(<span class="string">'.'</span>)
    , <span class="variable">obj</span> = <span class="this">this</span>.<span class="variable">_doc</span>;

  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="variable">pieces</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
    <span class="variable">obj</span> = <span class="keyword">null</span> == <span class="variable">obj</span> ? <span class="keyword">null</span> : <span class="variable">obj</span>[<span class="variable">pieces</span>[<span class="variable">i</span>]];
  }

  <span class="keyword">if</span> (<span class="variable">schema</span>) {
    <span class="variable">obj</span> = <span class="variable">schema</span>.<span class="variable">applyGetters</span>(<span class="variable">obj</span>, <span class="this">this</span>);
  }

  <span class="keyword">return</span> <span class="variable">obj</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Commits a path, marking as modified if needed. Useful for mixed keys</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">markModified</span> = <span class="keyword">function</span> (<span class="variable">path</span>) {
  <span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">modify</span>(<span class="variable">path</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>commit</p>

<p>Alias on markModified</p>

<ul><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">commit</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Document#commit'</span>
          , <span class="string">'Document#markModified'</span>
          , <span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">markModified</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Captures an exception that will be bubbled to <code>save</code></p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  function to execute</p></li><li><p><strong>param</strong>: <em>Object</em>  scope</p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="keyword">try</span> = <span class="keyword">function</span> (<span class="variable">fn</span>, <span class="variable">scope</span>) {
  <span class="keyword">var</span> <span class="variable">res</span>;
  <span class="keyword">try</span> {
    <span class="variable">fn</span>.<span class="variable">call</span>(<span class="variable">scope</span>);
    <span class="variable">res</span> = <span class="variable">true</span>;
  } <span class="keyword">catch</span> (<span class="variable">e</span>) {
    <span class="this">this</span>.<span class="variable">error</span>(<span class="variable">e</span>);
    <span class="variable">res</span> = <span class="variable">false</span>;
  }
  <span class="keyword">return</span> <span class="variable">res</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>modifiedPaths</p>

<p>Returns the list of paths that have been modified.</p>

<p>If we set <code>documents.0.title</code> to 'newTitle'
then <code>documents</code>, <code>documents.0</code>, and <code>documents.0.title</code>
are modified.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li><li><p><strong>returns</strong>: <em>Boolean</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'modifiedPaths'</span>, <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">directModifiedPaths</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">states</span>.<span class="variable">modify</span>);

  <span class="keyword">return</span> <span class="variable">directModifiedPaths</span>.<span class="variable">reduce</span>(<span class="keyword">function</span> (<span class="variable">list</span>, <span class="variable">path</span>) {
    <span class="keyword">var</span> <span class="variable">parts</span> = <span class="variable">path</span>.<span class="variable">split</span>(<span class="string">'.'</span>);
    <span class="keyword">return</span> <span class="variable">list</span>.<span class="variable">concat</span>(<span class="variable">parts</span>.<span class="variable">reduce</span>(<span class="keyword">function</span> (<span class="variable">chains</span>, <span class="variable">part</span>, <span class="variable">i</span>) {
      <span class="keyword">return</span> <span class="variable">chains</span>.<span class="variable">concat</span>(<span class="variable">parts</span>.<span class="variable">slice</span>(<span class="number integer">0</span>, <span class="variable">i</span>).<span class="variable">concat</span>(<span class="variable">part</span>).<span class="variable">join</span>(<span class="string">'.'</span>));
    }, []));
  }, []);
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Checks if a path or any full path containing path as part of
its path chain has been directly modified.</p>

<p>e.g., if we set <code>documents.0.title</code> to 'newTitle'
      then we have directly modified <code>documents.0.title</code>
      but not directly modified <code>documents</code> or <code>documents.0</code>.
      Nonetheless, we still say <code>documents</code> and <code>documents.0</code>
      are modified. They just are not considered direct modified.
      The distinction is important because we need to distinguish
      between what has been directly modified and what hasn't so
      that we can determine the MINIMUM set of dirty data
      that we want to send to MongoDB on a Document save.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>returns</strong>: <em>Boolean</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">isModified</span> = <span class="keyword">function</span> (<span class="variable">path</span>) {
  <span class="keyword">return</span> !!~<span class="this">this</span>.<span class="variable">modifiedPaths</span>.<span class="variable">indexOf</span>(<span class="variable">path</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Checks if a path has been directly set and modified. False if
the path is only part of a larger path that was directly set.</p>

<p>e.g., if we set <code>documents.0.title</code> to 'newTitle'
      then we have directly modified <code>documents.0.title</code>
      but not directly modified <code>documents</code> or <code>documents.0</code>.
      Nonetheless, we still say <code>documents</code> and <code>documents.0</code>
      are modified. They just are not considered direct modified.
      The distinction is important because we need to distinguish
      between what has been directly modified and what hasn't so
      that we can determine the MINIMUM set of dirty data
      that we want to send to MongoDB on a Document save.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>returns</strong>: <em>Boolean</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">isDirectModified</span> = <span class="keyword">function</span> (<span class="variable">path</span>) {
  <span class="keyword">return</span> (<span class="variable">path</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">states</span>.<span class="variable">modify</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Checks if a certain path was initialized</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>returns</strong>: <em>Boolean</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">isInit</span> = <span class="keyword">function</span> (<span class="variable">path</span>) {
  <span class="keyword">return</span> (<span class="variable">path</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">states</span>.<span class="variable">init</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Checks if a path was selected.
## </p>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>return</strong>: <em>Boolean</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">isSelected</span> = <span class="keyword">function</span> <span class="variable">isSelected</span> (<span class="variable">path</span>) {
  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">_selected</span>) {

    <span class="keyword">if</span> (<span class="string">'_id'</span> === <span class="variable">path</span>) {
      <span class="keyword">return</span> <span class="number integer">0</span> !== <span class="this">this</span>.<span class="variable">_selected</span>.<span class="variable">_id</span>;
    }

    <span class="keyword">var</span> <span class="variable">paths</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">_selected</span>)
      , <span class="variable">i</span> = <span class="variable">paths</span>.<span class="variable">length</span>
      , <span class="variable">inclusive</span> = <span class="variable">false</span>
      , <span class="variable">cur</span>

    <span class="keyword">if</span> (<span class="number integer">1</span> === <span class="variable">i</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'_id'</span> === <span class="variable">paths</span>[<span class="number integer">0</span>]) {
      <span class="comment">// only _id was selected.</span>
      <span class="keyword">return</span> <span class="number integer">0</span> === <span class="this">this</span>.<span class="variable">_selected</span>.<span class="variable">_id</span>;
    }

    <span class="keyword">while</span> (<span class="variable">i</span>--) {
      <span class="variable">cur</span> = <span class="variable">paths</span>[<span class="variable">i</span>];
      <span class="keyword">if</span> (<span class="string">'_id'</span> == <span class="variable">cur</span>) <span class="keyword">continue</span>;
      <span class="variable">inclusive</span> = !! <span class="this">this</span>.<span class="variable">_selected</span>[<span class="variable">cur</span>];
      <span class="keyword">break</span>;
    }

    <span class="keyword">if</span> (<span class="variable">path</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">_selected</span>) {
      <span class="keyword">return</span> <span class="variable">inclusive</span>;
    }

    <span class="variable">i</span> = <span class="variable">paths</span>.<span class="variable">length</span>;

    <span class="keyword">while</span> (<span class="variable">i</span>--) {
      <span class="variable">cur</span> = <span class="variable">paths</span>[<span class="variable">i</span>];
      <span class="keyword">if</span> (<span class="string">'_id'</span> == <span class="variable">cur</span>) <span class="keyword">continue</span>;

      <span class="keyword">if</span> (<span class="number integer">0</span> === <span class="variable">cur</span>.<span class="variable">indexOf</span>(<span class="variable">path</span> + <span class="string">'.'</span>)) {
        <span class="keyword">return</span> <span class="variable">inclusive</span>;
      }

      <span class="keyword">if</span> (<span class="number integer">0</span> === (<span class="variable">path</span> + <span class="string">'.'</span>).<span class="variable">indexOf</span>(<span class="variable">cur</span>)) {
        <span class="keyword">return</span> <span class="variable">inclusive</span>;
      }
    }

    <span class="keyword">return</span> ! <span class="variable">inclusive</span>;
  }

  <span class="keyword">return</span> <span class="variable">true</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Validation middleware</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  next</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">validate</span> = <span class="keyword">function</span> (<span class="variable">next</span>) {
  <span class="keyword">var</span> <span class="variable">total</span> = <span class="number integer">0</span>
    , <span class="variable">self</span> = <span class="this">this</span>
    , <span class="variable">validating</span> = {}

  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">some</span>(<span class="string">'require'</span>, <span class="string">'init'</span>, <span class="string">'modify'</span>)) {
    <span class="keyword">return</span> <span class="variable">complete</span>();
  }

  <span class="keyword">function</span> <span class="variable">complete</span> () {
    <span class="variable">next</span>(<span class="variable">self</span>.<span class="variable">_validationError</span>);
    <span class="variable">self</span>.<span class="variable">_validationError</span> = <span class="keyword">null</span>;
  }

  <span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">forEach</span>(<span class="string">'require'</span>, <span class="string">'init'</span>, <span class="string">'modify'</span>, <span class="keyword">function</span> <span class="variable">validatePath</span> (<span class="variable">path</span>) {
    <span class="keyword">if</span> (<span class="variable">validating</span>[<span class="variable">path</span>]) <span class="keyword">return</span>;

    <span class="variable">validating</span>[<span class="variable">path</span>] = <span class="variable">true</span>;
    <span class="variable">total</span>++;

    <span class="variable">process</span>.<span class="variable">nextTick</span>(<span class="keyword">function</span>(){
      <span class="keyword">var</span> <span class="variable">p</span> = <span class="variable">self</span>.<span class="variable">schema</span>.<span class="variable">path</span>(<span class="variable">path</span>);
      <span class="keyword">if</span> (!<span class="variable">p</span>) <span class="keyword">return</span> --<span class="variable">total</span> || <span class="variable">complete</span>();

      <span class="variable">p</span>.<span class="variable">doValidate</span>(<span class="variable">self</span>.<span class="variable">getValue</span>(<span class="variable">path</span>), <span class="keyword">function</span> (<span class="variable">err</span>) {
        <span class="keyword">if</span> (<span class="variable">err</span>) <span class="variable">self</span>.<span class="variable">invalidate</span>(<span class="variable">path</span>, <span class="variable">err</span>);
        --<span class="variable">total</span> || <span class="variable">complete</span>();
      }, <span class="variable">self</span>);
    });
  });

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Marks a path as invalid, causing a subsequent validation to fail.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path of the field to invalidate</p></li><li><p><strong>param</strong>: <em>String | Error</em>  error of the path.</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">invalidate</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">err</span>) {
  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">_validationError</span>) {
    <span class="this">this</span>.<span class="variable">_validationError</span> = <span class="keyword">new</span> <span class="class">ValidationError</span>(<span class="this">this</span>);
  }

  <span class="keyword">if</span> (!<span class="variable">err</span> || <span class="string">'string'</span> === <span class="keyword">typeof</span> <span class="variable">err</span>) {
    <span class="variable">err</span> = <span class="keyword">new</span> <span class="class">ValidatorError</span>(<span class="variable">path</span>, <span class="variable">err</span>);
  }

  <span class="this">this</span>.<span class="variable">_validationError</span>.<span class="variable">errors</span>[<span class="variable">path</span>] = <span class="variable">err</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns if the document has been modified</p>

<h2></h2>

<ul><li><p><strong>return</strong>: <em>Boolean</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'modified'</span>, <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">_activePaths</span>.<span class="variable">some</span>(<span class="string">'modify'</span>);
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Gets the document</p>

<p>Available options:</p>

<ul><li>getters: apply all getters (path and virtual getters)</li><li>virtuals: apply virtual getters (can override <code>getters</code> option)</li></ul>

<p>Example of only applying path getters:</p>

<pre><code>doc.toObject({ getters: true, virtuals: false })</code></pre>

<p>Example of only applying virtual getters:</p>

<pre><code>doc.toObject({ virtuals: true })</code></pre>

<p>Example of applying both path and virtual getters:</p>

<pre><code>doc.toObject({ getters: true })</code></pre>

<h2></h2>

<ul><li><p><strong>return</strong>: <em>Object</em>  plain object</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">toObject</span> = <span class="keyword">function</span> (<span class="variable">options</span>) {
  <span class="variable">options</span> || (<span class="variable">options</span> = {});
  <span class="variable">options</span>.<span class="variable">minimize</span> = <span class="variable">true</span>;

  <span class="keyword">var</span> <span class="variable">ret</span> = <span class="variable">clone</span>(<span class="this">this</span>.<span class="variable">_doc</span>, <span class="variable">options</span>);

  <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">virtuals</span> || <span class="variable">options</span>.<span class="variable">getters</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">false</span> !== <span class="variable">options</span>.<span class="variable">virtuals</span>) {
    <span class="variable">applyGetters</span>(<span class="this">this</span>, <span class="variable">ret</span>, <span class="string">'virtuals'</span>, <span class="variable">options</span>);
  }

  <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">getters</span>) {
    <span class="variable">applyGetters</span>(<span class="this">this</span>, <span class="variable">ret</span>, <span class="string">'paths'</span>, <span class="variable">options</span>);
  }

  <span class="keyword">return</span> <span class="variable">ret</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>JSON.stringify helper.</p>

<p>Implicitly called when a document is passed
to JSON.stringify()</p>

<h2></h2>

<ul><li><p><strong>return</strong>: <em>Object</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">toJSON</span> = <span class="keyword">function</span> (<span class="variable">options</span>) {
  <span class="keyword">if</span> (<span class="string">'undefined'</span> === <span class="keyword">typeof</span> <span class="variable">options</span>) <span class="variable">options</span> = {};
  <span class="variable">options</span>.<span class="variable">json</span> = <span class="variable">true</span>;
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">toObject</span>(<span class="variable">options</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Helper for console.log</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">toString</span> =
<span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">inspect</span> = <span class="keyword">function</span> (<span class="variable">options</span>) {
  <span class="keyword">return</span> <span class="variable">inspect</span>(<span class="this">this</span>.<span class="variable">toObject</span>(<span class="variable">options</span>));
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns true if the Document stores the same data as doc.
## </p>

<ul><li><p><strong>param</strong>: <em>Document</em>  doc to compare to</p></li><li><p><strong>return</strong>: <em>Boolean</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">equals</span> = <span class="keyword">function</span> (<span class="variable">doc</span>) {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">get</span>(<span class="string">'_id'</span>) === <span class="variable">doc</span>.<span class="variable">get</span>(<span class="string">'_id'</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">Document</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Document Validation Error
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">ValidationError</span> (<span class="variable">instance</span>) {
  <span class="class">MongooseError</span>.<span class="variable">call</span>(<span class="this">this</span>, &<span class="variable">quot</span>;<span class="class">Validation</span> <span class="variable">failed</span>&<span class="variable">quot</span>;);
  <span class="class">Error</span>.<span class="variable">captureStackTrace</span>(<span class="this">this</span>, <span class="variable">arguments</span>.<span class="variable">callee</span>);
  <span class="this">this</span>.<span class="variable">name</span> = <span class="string">'ValidationError'</span>;
  <span class="this">this</span>.<span class="variable">errors</span> = <span class="variable">instance</span>.<span class="variable">errors</span> = {};
};

<span class="class">ValidationError</span>.<span class="variable">prototype</span>.<span class="variable">toString</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">name</span> + <span class="string">': '</span> + <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">errors</span>).<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">key</span>) {
    <span class="keyword">return</span> <span class="class">String</span>(<span class="this">this</span>.<span class="variable">errors</span>[<span class="variable">key</span>]);
  }, <span class="this">this</span>).<span class="variable">join</span>(<span class="string">', '</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from MongooseError.
 </p>
</td>
<td class="code">
<pre><code><span class="class">ValidationError</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">MongooseError</span>.<span class="variable">prototype</span>;

<span class="class">Document</span>.<span class="class">ValidationError</span> = <span class="class">ValidationError</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Document Error</p>

<ul><li><p><strong>param</strong>: <em>text</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">DocumentError</span> () {
  <span class="class">MongooseError</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">msg</span>);
  <span class="class">Error</span>.<span class="variable">captureStackTrace</span>(<span class="this">this</span>, <span class="variable">arguments</span>.<span class="variable">callee</span>);
  <span class="this">this</span>.<span class="variable">name</span> = <span class="string">'DocumentError'</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from MongooseError.
 </p>
</td>
<td class="code">
<pre><code><span class="class">DocumentError</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">MongooseError</span>.<span class="variable">prototype</span>;

<span class="variable">exports</span>.<span class="class">Error</span> = <span class="class">DocumentError</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/drivers/node-mongodb-native/binary.js"><a href="#">binary</a></h2></td><td>lib/drivers/node-mongodb-native/binary.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Binary</span> = <span class="variable">require</span>(<span class="string">'mongodb'</span>).<span class="class">BSONPure</span>.<span class="class">Binary</span>;

<span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">exports</span> = <span class="class">Binary</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/drivers/node-mongodb-native/collection.js"><a href="#">collection</a></h2></td><td>lib/drivers/node-mongodb-native/collection.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Collection</span> = <span class="variable">require</span>(<span class="string">'../../collection'</span>)
  , <span class="class">NativeCollection</span> = <span class="variable">require</span>(<span class="string">'mongodb'</span>).<span class="class">Collection</span>
  , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'../../utils'</span>)</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from abstract Collection.
 </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseCollection</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">Collection</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Copy the collection methods and make them subject to queues
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="class">NativeCollection</span>.<span class="variable">prototype</span>)
  (<span class="keyword">function</span>(<span class="variable">i</span>){
    <span class="class">MongooseCollection</span>.<span class="variable">prototype</span>[<span class="variable">i</span>] = <span class="keyword">function</span> () {
      <span class="comment">// BENCHMARKME: is it worth caching the prototype methods? probably</span>
      <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">buffer</span>) {
        <span class="keyword">var</span> <span class="variable">collection</span> = <span class="this">this</span>.<span class="variable">collection</span>
          , <span class="variable">args</span> = <span class="variable">arguments</span>
          , <span class="variable">self</span> = <span class="this">this</span>;

        <span class="variable">process</span>.<span class="variable">nextTick</span>(<span class="keyword">function</span>(){
          <span class="keyword">var</span> <span class="variable">debug</span> = <span class="variable">self</span>.<span class="variable">conn</span>.<span class="variable">base</span>.<span class="variable">options</span>.<span class="variable">debug</span>;

          <span class="keyword">if</span> (<span class="variable">debug</span>) {
            <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">debug</span>) {
              <span class="variable">debug</span>.<span class="variable">apply</span>(<span class="variable">debug</span>
                , [<span class="variable">self</span>.<span class="variable">name</span>, <span class="variable">i</span>].<span class="variable">concat</span>(<span class="variable">utils</span>.<span class="variable">args</span>(<span class="variable">args</span>, <span class="number integer">0</span>, <span class="variable">args</span>.<span class="variable">length</span>-<span class="number integer">1</span>)));
            } <span class="keyword">else</span> {
              <span class="variable">console</span>.<span class="variable">error</span>(<span class="string">'\x1B[0;36mMongoose:\x1B[0m %s.%s(%s) %s %s %s'</span>
                , <span class="variable">self</span>.<span class="variable">name</span>
                , <span class="variable">i</span>
                , <span class="variable">print</span>(<span class="variable">args</span>[<span class="number integer">0</span>])
                , <span class="variable">print</span>(<span class="variable">args</span>[<span class="number integer">1</span>])
                , <span class="variable">print</span>(<span class="variable">args</span>[<span class="number integer">2</span>])
                , <span class="variable">print</span>(<span class="variable">args</span>[<span class="number integer">3</span>]))
            }
          }

          <span class="variable">collection</span>[<span class="variable">i</span>].<span class="variable">apply</span>(<span class="variable">collection</span>, <span class="variable">args</span>);
        });
      } <span class="keyword">else</span> {
        <span class="this">this</span>.<span class="variable">addQueue</span>(<span class="variable">i</span>, <span class="variable">arguments</span>);
      }
    };
  })(<span class="variable">i</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Override signature of ensureIndex. -native one is not standard.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  spec</p></li><li><p><strong>param</strong>: <em>Object</em>  options</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">oldEnsureIndex</span> = <span class="class">NativeCollection</span>.<span class="variable">prototype</span>.<span class="variable">ensureIndex</span>;

<span class="keyword">function</span> <span class="variable">noop</span> () {};

<span class="class">NativeCollection</span>.<span class="variable">prototype</span>.<span class="variable">ensureIndex</span> = <span class="keyword">function</span>(<span class="variable">fields</span>, <span class="variable">options</span>, <span class="variable">fn</span>){
  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">buffer</span>) {
    <span class="keyword">return</span> <span class="variable">oldEnsureIndex</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
  }
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">MongooseCollection</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/drivers/node-mongodb-native/connection.js"><a href="#">connection</a></h2></td><td>lib/drivers/node-mongodb-native/connection.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Connection</span> = <span class="variable">require</span>(<span class="string">'../../connection'</span>)
  , <span class="variable">mongo</span> = <span class="variable">require</span>(<span class="string">'mongodb'</span>)
  , <span class="class">Server</span> = <span class="variable">mongo</span>.<span class="class">Server</span>
  , <span class="class">ReplSetServers</span> = <span class="variable">mongo</span>.<span class="class">ReplSetServers</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from Connection.
 </p>
</td>
<td class="code">
<pre><code><span class="class">NativeConnection</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">Connection</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">NativeConnection</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/drivers/node-mongodb-native/objectid.js"><a href="#">objectid</a></h2></td><td>lib/drivers/node-mongodb-native/objectid.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">ObjectId</span> = <span class="variable">require</span>(<span class="string">'mongodb'</span>).<span class="class">BSONPure</span>.<span class="class">ObjectID</span>;</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/error.js"><a href="#">error</a></h2></td><td>lib/error.js</td></tr><tr class="code">
<td class="docs">
<p>Inherits from Error.
 </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseError</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">Error</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">MongooseError</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/index.js"><a href="#">index</a></h2></td><td>lib/index.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Schema</span> = <span class="variable">require</span>(<span class="string">'./schema'</span>)
  , <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'./schematype'</span>)
  , <span class="class">VirtualType</span> = <span class="variable">require</span>(<span class="string">'./virtualtype'</span>)
  , <span class="class">SchemaTypes</span> = <span class="class">Schema</span>.<span class="class">Types</span>
  , <span class="class">SchemaDefaults</span> = <span class="variable">require</span>(<span class="string">'./schemadefault'</span>)
  , <span class="class">Types</span> = <span class="variable">require</span>(<span class="string">'./types'</span>)
  , <span class="class">Query</span> = <span class="variable">require</span>(<span class="string">'./query'</span>)
  , <span class="class">Promise</span> = <span class="variable">require</span>(<span class="string">'./promise'</span>)
  , <span class="class">Model</span> = <span class="variable">require</span>(<span class="string">'./model'</span>)
  , <span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'./document'</span>)
  , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Mongoose constructor. Most apps will only use one instance.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">Mongoose</span> () {
  <span class="this">this</span>.<span class="variable">connections</span> = [];
  <span class="this">this</span>.<span class="variable">plugins</span> = [];
  <span class="this">this</span>.<span class="variable">models</span> = {};
  <span class="this">this</span>.<span class="variable">modelSchemas</span> = {};
  <span class="this">this</span>.<span class="variable">options</span> = {};
  <span class="this">this</span>.<span class="variable">createConnection</span>(); <span class="comment">// default connection</span>
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets/gets mongoose options</p>

<h2>Examples</h2>

<p>   mongoose.set('test') // returns the 'test' value
   mongoose.set('test', value) // sets the 'test' value</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  key</p></li><li><p><strong>param</strong>: <em>String</em>  value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">set</span> =
<span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">key</span>, <span class="variable">value</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> == <span class="number integer">1</span>)
    <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">options</span>[<span class="variable">key</span>];
  <span class="this">this</span>.<span class="variable">options</span>[<span class="variable">key</span>] = <span class="variable">value</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Creates a Connection instance.</p>

<h2>Examples</h2>

<p>   // with mongodb:// URI
   db = mongoose.createConnection('mongodb://localhost:port/database');</p>

<p>   // with [host, database_name[, port] signature 
   db = mongoose.createConnection('localhost', 'database', port)</p>

<p>   // initialize now, connect later
   db = mongoose.createConnection();
   db.open('localhost', 'database', port);</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  mongodb:// URI</p></li><li><p><strong>return</strong>: <em>Connection</em>  the created Connection object</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">createConnection</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">conn</span> = <span class="keyword">new</span> <span class="class">Connection</span>(<span class="this">this</span>);
  <span class="this">this</span>.<span class="variable">connections</span>.<span class="variable">push</span>(<span class="variable">conn</span>);
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span>)
    <span class="variable">conn</span>.<span class="variable">open</span>.<span class="variable">apply</span>(<span class="variable">conn</span>, <span class="variable">arguments</span>);
  <span class="keyword">return</span> <span class="variable">conn</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Creates a replica set connection</p>

<h2></h2>

<ul><li><p><strong>see</strong>: <em>Mongoose#createConnection</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">createSetConnection</span> () {
  <span class="keyword">var</span> <span class="variable">conn</span> = <span class="keyword">new</span> <span class="class">Connection</span>(<span class="this">this</span>);
  <span class="this">this</span>.<span class="variable">connections</span>.<span class="variable">push</span>(<span class="variable">conn</span>);
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span>)
    <span class="variable">conn</span>.<span class="variable">openSet</span>.<span class="variable">apply</span>(<span class="variable">conn</span>, <span class="variable">arguments</span>);
  <span class="keyword">return</span> <span class="variable">conn</span>;
};

<span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">createSetConnection</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Mongoose#createSetConnection'</span>
          , <span class="string">'Mongoose#createConnection in v3'</span>
          , <span class="variable">createSetConnection</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connects the default mongoose connection</p>

<h2></h2>

<ul><li><p><strong>see</strong>: <em>Mongoose#createConnection</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">connect</span> = <span class="keyword">function</span> (){
  <span class="this">this</span>.<span class="variable">connection</span>.<span class="variable">open</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">connection</span>, <span class="variable">arguments</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connects the default mongoose connection to a replica set</p>

<h2></h2>

<ul><li><p><strong>see</strong>: <em>Mongoose#createSetConnection</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">connectSet</span> () {
  <span class="this">this</span>.<span class="variable">connection</span>.<span class="variable">openSet</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">connection</span>, <span class="variable">arguments</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">connectSet</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Mongoose#connectSet'</span>, <span class="string">'Mongoose#connect in v3'</span>, <span class="variable">connectSet</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Disconnects from all connections.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  optional callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">disconnect</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">var</span> <span class="variable">count</span> = <span class="this">this</span>.<span class="variable">connections</span>.<span class="variable">length</span>;
  <span class="this">this</span>.<span class="variable">connections</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">conn</span>){
    <span class="variable">conn</span>.<span class="variable">close</span>(<span class="keyword">function</span>(<span class="variable">err</span>){
      <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">fn</span>(<span class="variable">err</span>);
      <span class="keyword">if</span> (<span class="variable">fn</span>)
        --<span class="variable">count</span> || <span class="variable">fn</span>();
    });
  });
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Defines a model or retrieves it</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  model name</p></li><li><p><strong>param</strong>: <em>Schema</em>  schema object</p></li><li><p><strong>param</strong>: <em>String</em>  collection name (optional, induced from model name)</p></li><li><p><strong>param</strong>: <em>Boolean</em>  whether to skip initialization (defaults to false)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">model</span> = <span class="keyword">function</span> (<span class="variable">name</span>, <span class="variable">schema</span>, <span class="variable">collection</span>, <span class="variable">skipInit</span>) {
  <span class="comment">// normalize collection</span>
  <span class="keyword">if</span> (!(<span class="variable">schema</span> <span class="variable">instanceof</span> <span class="class">Schema</span>)) {
    <span class="variable">collection</span> = <span class="variable">schema</span>;
    <span class="variable">schema</span> = <span class="variable">false</span>;
  }

  <span class="keyword">if</span> (<span class="string">'boolean'</span> === <span class="keyword">typeof</span> <span class="variable">collection</span>) {
    <span class="variable">skipInit</span> = <span class="variable">collection</span>;
    <span class="variable">collection</span> = <span class="keyword">null</span>;
  }

  <span class="comment">// look up models for the collection</span>
  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">modelSchemas</span>[<span class="variable">name</span>]) {
    <span class="keyword">if</span> (!<span class="variable">schema</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">name</span> <span class="keyword">in</span> <span class="class">SchemaDefaults</span>) {
      <span class="variable">schema</span> = <span class="class">SchemaDefaults</span>[<span class="variable">name</span>];
    }

    <span class="keyword">if</span> (<span class="variable">schema</span>) {
      <span class="this">this</span>.<span class="variable">modelSchemas</span>[<span class="variable">name</span>] = <span class="variable">schema</span>;
      <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="this">this</span>.<span class="variable">plugins</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
        <span class="variable">schema</span>.<span class="variable">plugin</span>(<span class="this">this</span>.<span class="variable">plugins</span>[<span class="variable">i</span>][<span class="number integer">0</span>], <span class="this">this</span>.<span class="variable">plugins</span>[<span class="variable">i</span>][<span class="number integer">1</span>]);
      }
    } <span class="keyword">else</span> {
      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="variable">t</span> <span class="variable">been</span> <span class="variable">registered</span> <span class="keyword">for</span> <span class="variable">model</span> &<span class="variable">quot</span>;<span class="string">' + name + '</span>&<span class="variable">quot</span>;.\<span class="variable">n</span>'
                    + <span class="string">'Use mongoose.model(name, schema)'</span>);
    }
  }

  <span class="keyword">if</span> (!<span class="variable">schema</span>) {
    <span class="variable">schema</span> = <span class="this">this</span>.<span class="variable">modelSchemas</span>[<span class="variable">name</span>];
  }

  <span class="keyword">if</span> (!<span class="variable">collection</span>) {
    <span class="variable">collection</span> = <span class="variable">schema</span>.<span class="variable">set</span>(<span class="string">'collection'</span>) || <span class="variable">utils</span>.<span class="variable">toCollectionName</span>(<span class="variable">name</span>);
  }

  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">models</span>[<span class="variable">name</span>]) {
    <span class="keyword">var</span> <span class="variable">model</span> = <span class="class">Model</span>.<span class="variable">compile</span>(<span class="variable">name</span>
                        , <span class="this">this</span>.<span class="variable">modelSchemas</span>[<span class="variable">name</span>]
                        , <span class="variable">collection</span>
                        , <span class="this">this</span>.<span class="variable">connection</span>
                        , <span class="this">this</span>);

    <span class="keyword">if</span> (!<span class="variable">skipInit</span>) <span class="variable">model</span>.<span class="variable">init</span>();

    <span class="this">this</span>.<span class="variable">models</span>[<span class="variable">name</span>] = <span class="variable">model</span>;
  }

  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">models</span>[<span class="variable">name</span>];
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Declares a plugin executed on Schemas. Equivalent to calling <code>.plugin(fn)</code>
on each Schema you create.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  plugin callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">plugin</span> = <span class="keyword">function</span> (<span class="variable">fn</span>, <span class="variable">opts</span>) {
  <span class="this">this</span>.<span class="variable">plugins</span>.<span class="variable">push</span>([<span class="variable">fn</span>, <span class="variable">opts</span>]);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Default connection</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Mongoose</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'connection'</span>, <span class="keyword">function</span>(){
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">connections</span>[<span class="number integer">0</span>];
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Driver depentend APIs
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">driver</span> = <span class="variable">global</span>.<span class="class">MONGOOSE_DRIVER_PATH</span> || <span class="string">'./drivers/node-mongodb-native'</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connection</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Connection</span> = <span class="variable">require</span>(<span class="variable">driver</span> + <span class="string">'/connection'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Collection</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Collection</span> = <span class="variable">require</span>(<span class="variable">driver</span> + <span class="string">'/collection'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export default singleton.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">exports</span> = <span class="keyword">new</span> <span class="class">Mongoose</span>();</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Collection</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Collection</span> = <span class="class">Collection</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connection</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Connection</span> = <span class="class">Connection</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Exports Mongoose version</p>

<ul><li><p><strong>param</strong>: <em>version</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="variable">version</span> = <span class="class">JSON</span>.<span class="variable">parse</span>(
  <span class="variable">require</span>(<span class="string">'fs'</span>).<span class="variable">readFileSync</span>(<span class="variable">__dirname</span> + <span class="string">'/../package.json'</span>, <span class="string">'utf8'</span>)
).<span class="variable">version</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export Mongoose constructor</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Mongoose</span> = <span class="class">Mongoose</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export Schema constructor</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Schema</span> = <span class="class">Schema</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export SchemaType constructor.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">SchemaType</span> = <span class="class">SchemaType</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export VirtualType constructor.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">VirtualType</span> = <span class="class">VirtualType</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export Schema types</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">SchemaTypes</span> = <span class="class">SchemaTypes</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export types</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Types</span> = <span class="class">Types</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export Query</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Query</span> = <span class="class">Query</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export Promise</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Promise</span> = <span class="class">Promise</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export Model constructor</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Model</span> = <span class="class">Model</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export Document constructor</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Document</span> = <span class="class">Document</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export MongooseError</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Error</span> = <span class="variable">require</span>(<span class="string">'./error'</span>);

<span class="variable">exports</span>.<span class="variable">mongo</span> = <span class="variable">require</span>(<span class="string">'mongodb'</span>);
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/model.js"><a href="#">model</a></h2></td><td>lib/model.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'./document'</span>)
  , <span class="class">MongooseArray</span> = <span class="variable">require</span>(<span class="string">'./types/array'</span>)
  , <span class="class">MongooseBuffer</span> = <span class="variable">require</span>(<span class="string">'./types/buffer'</span>)
  , <span class="class">MongooseError</span> = <span class="variable">require</span>(<span class="string">'./error'</span>)
  , <span class="class">Query</span> = <span class="variable">require</span>(<span class="string">'./query'</span>)
  , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>)
  , <span class="variable">isMongooseObject</span> = <span class="variable">utils</span>.<span class="variable">isMongooseObject</span>
  , <span class="class">EventEmitter</span> = <span class="variable">utils</span>.<span class="class">EventEmitter</span>
  , <span class="variable">merge</span> = <span class="variable">utils</span>.<span class="variable">merge</span>
  , <span class="class">Promise</span> = <span class="variable">require</span>(<span class="string">'./promise'</span>)
  , <span class="variable">tick</span> = <span class="variable">utils</span>.<span class="variable">tick</span></code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Model constructor</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  values to set</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">Model</span> (<span class="variable">doc</span>, <span class="variable">fields</span>) {
  <span class="class">Document</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">doc</span>, <span class="variable">fields</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from Document.
 </p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">Document</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Connection the model uses. Set by the Connection or if absent set to the
default mongoose connection;</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">db</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Collection the model uses. Set by Mongoose instance</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">collection</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Model name.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">modelName</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Saves this document.</p>

<ul><li><p><strong>see</strong>: <em>Model</em>
#registerHooks
## </p></li><li><p><strong>param</strong>: <em>Function</em>  fn</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">save</span> = <span class="keyword">function</span> <span class="variable">save</span> (<span class="variable">fn</span>) {
  <span class="keyword">var</span> <span class="variable">promise</span> = <span class="keyword">new</span> <span class="class">Promise</span>(<span class="variable">fn</span>)
    , <span class="variable">complete</span> = <span class="variable">handleSave</span>(<span class="variable">promise</span>, <span class="this">this</span>)
    , <span class="variable">options</span> = {}

  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">safe</span>) {
    <span class="variable">options</span>.<span class="variable">safe</span> = <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">safe</span>;
  }

  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">isNew</span>) {
    <span class="comment">// send entire doc</span>
    <span class="this">this</span>.<span class="variable">collection</span>.<span class="variable">insert</span>(<span class="this">this</span>.<span class="variable">toObject</span>({ <span class="variable">depopulate</span>: <span class="number integer">1</span> }), <span class="variable">options</span>, <span class="variable">complete</span>);
    <span class="this">this</span>.<span class="variable">_reset</span>();
    <span class="this">this</span>.<span class="variable">isNew</span> = <span class="variable">false</span>;
    <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'isNew'</span>, <span class="variable">false</span>);
    <span class="comment">// Make it possible to retry the insert</span>
    <span class="this">this</span>.<span class="variable">_inserting</span> = <span class="variable">true</span>;

  } <span class="keyword">else</span> {
    <span class="comment">// Make sure we don't treat it as a new object on error,</span>
    <span class="comment">// since it already exists</span>
    <span class="this">this</span>.<span class="variable">_inserting</span> = <span class="variable">false</span>;

    <span class="keyword">var</span> <span class="variable">delta</span> = <span class="this">this</span>.<span class="variable">_delta</span>();
    <span class="this">this</span>.<span class="variable">_reset</span>();

    <span class="keyword">if</span> (<span class="variable">delta</span>) {
      <span class="keyword">var</span> <span class="variable">where</span> = <span class="this">this</span>.<span class="variable">_where</span>();
      <span class="this">this</span>.<span class="variable">collection</span>.<span class="variable">update</span>(<span class="variable">where</span>, <span class="variable">delta</span>, <span class="variable">options</span>, <span class="variable">complete</span>);
    } <span class="keyword">else</span> {
      <span class="variable">complete</span>(<span class="keyword">null</span>);
    }

    <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'isNew'</span>, <span class="variable">false</span>);
  }
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove the document</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> <span class="variable">remove</span> (<span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">_removing</span>) <span class="keyword">return</span> <span class="this">this</span>;

  <span class="keyword">var</span> <span class="variable">promise</span> = <span class="this">this</span>.<span class="variable">_removing</span> = <span class="keyword">new</span> <span class="class">Promise</span>(<span class="variable">fn</span>)
    , <span class="variable">where</span> = <span class="this">this</span>.<span class="variable">_where</span>()
    , <span class="variable">self</span> = <span class="this">this</span>
    , <span class="variable">options</span> = {}

  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">safe</span>) {
    <span class="variable">options</span>.<span class="variable">safe</span> = <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">safe</span>;
  }

  <span class="this">this</span>.<span class="variable">collection</span>.<span class="variable">remove</span>(<span class="variable">where</span>, <span class="variable">options</span>, <span class="variable">tick</span>(<span class="keyword">function</span> (<span class="variable">err</span>) {
    <span class="keyword">if</span> (<span class="variable">err</span>) {
      <span class="variable">promise</span>.<span class="variable">error</span>(<span class="variable">err</span>);
      <span class="variable">promise</span> = <span class="variable">self</span> = <span class="variable">self</span>.<span class="variable">_removing</span> = <span class="variable">where</span> = <span class="variable">options</span> = <span class="keyword">null</span>;
      <span class="keyword">return</span>;
    }
    <span class="variable">promise</span>.<span class="variable">complete</span>();
    <span class="variable">self</span>.<span class="variable">emit</span>(<span class="string">'remove'</span>, <span class="variable">self</span>);
    <span class="variable">promise</span> = <span class="variable">self</span> = <span class="variable">where</span> = <span class="variable">options</span> = <span class="keyword">null</span>;
  }));

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Shortcut to access another model.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  model name</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">model</span> = <span class="keyword">function</span> <span class="variable">model</span> (<span class="variable">name</span>) {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">db</span>.<span class="variable">model</span>(<span class="variable">name</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Give the constructor the ability to emit events.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="class">EventEmitter</span>.<span class="variable">prototype</span>)
  <span class="class">Model</span>[<span class="variable">i</span>] = <span class="class">EventEmitter</span>.<span class="variable">prototype</span>[<span class="variable">i</span>];</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Document schema</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">schema</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Database instance the model uses.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">db</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Collection the model uses.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">collection</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Define properties that access the prototype.
 </p>
</td>
<td class="code">
<pre><code>[<span class="string">'db'</span>, <span class="string">'collection'</span>, <span class="string">'schema'</span>, <span class="string">'options'</span>, <span class="string">'model'</span>].<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">prop</span>){
  <span class="class">Model</span>.<span class="variable">__defineGetter__</span>(<span class="variable">prop</span>, <span class="keyword">function</span>(){
    <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">prototype</span>[<span class="variable">prop</span>];
  });
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">exports</span> = <span class="class">Model</span>;

<span class="class">Model</span>.<span class="variable">remove</span> = <span class="keyword">function</span> <span class="variable">remove</span> (<span class="variable">conditions</span>, <span class="variable">callback</span>) {
  <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">conditions</span>) {
    <span class="variable">callback</span> = <span class="variable">conditions</span>;
    <span class="variable">conditions</span> = {};
  }

  <span class="keyword">var</span> <span class="variable">query</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">conditions</span>).<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'remove'</span>);

  <span class="keyword">if</span> (<span class="string">'undefined'</span> === <span class="keyword">typeof</span> <span class="variable">callback</span>)
    <span class="keyword">return</span> <span class="variable">query</span>;

  <span class="this">this</span>.<span class="variable">_applyNamedScope</span>(<span class="variable">query</span>);
  <span class="keyword">return</span> <span class="variable">query</span>.<span class="variable">remove</span>(<span class="variable">callback</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Finds documents</p>

<h2>Examples</h2>

<p>   // retrieve only certain keys
   MyModel.find({ name: /john/i }, ['name', 'friends'], function () { })</p>

<p>   // pass options
   MyModel.find({ name: /john/i }, [], { skip: 10 } )</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  conditions</p></li><li><p><strong>param</strong>: <em>Object | Function</em>  (optional) fields to hydrate or callback</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">find</span> = <span class="keyword">function</span> <span class="variable">find</span> (<span class="variable">conditions</span>, <span class="variable">fields</span>, <span class="variable">options</span>, <span class="variable">callback</span>) {
  <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">conditions</span>) {
    <span class="variable">callback</span> = <span class="variable">conditions</span>;
    <span class="variable">conditions</span> = {};
    <span class="variable">fields</span> = <span class="keyword">null</span>;
    <span class="variable">options</span> = <span class="keyword">null</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">fields</span>) {
    <span class="variable">callback</span> = <span class="variable">fields</span>;
    <span class="variable">fields</span> = <span class="keyword">null</span>;
    <span class="variable">options</span> = <span class="keyword">null</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">options</span>) {
    <span class="variable">callback</span> = <span class="variable">options</span>;
    <span class="variable">options</span> = <span class="keyword">null</span>;
  }

  <span class="keyword">var</span> <span class="variable">query</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">conditions</span>, <span class="variable">options</span>);
  <span class="variable">query</span>.<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'find'</span>);
  <span class="variable">query</span>.<span class="variable">select</span>(<span class="variable">fields</span>);

  <span class="keyword">if</span> (<span class="string">'undefined'</span> === <span class="keyword">typeof</span> <span class="variable">callback</span>)
    <span class="keyword">return</span> <span class="variable">query</span>;

  <span class="this">this</span>.<span class="variable">_applyNamedScope</span>(<span class="variable">query</span>);
  <span class="keyword">return</span> <span class="variable">query</span>.<span class="variable">find</span>(<span class="variable">callback</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Finds by id</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>ObjectId | Object</em>  objectid, or a value that can be casted to it</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">findById</span> = <span class="keyword">function</span> <span class="variable">findById</span> (<span class="variable">id</span>, <span class="variable">fields</span>, <span class="variable">options</span>, <span class="variable">callback</span>) {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">findOne</span>({ <span class="variable">_id</span>: <span class="variable">id</span> }, <span class="variable">fields</span>, <span class="variable">options</span>, <span class="variable">callback</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Finds one document</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  conditions</p></li><li><p><strong>param</strong>: <em>Object | Function</em>  (optional) fields to hydrate or callback</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">findOne</span> = <span class="keyword">function</span> <span class="variable">findOne</span> (<span class="variable">conditions</span>, <span class="variable">fields</span>, <span class="variable">options</span>, <span class="variable">callback</span>) {
  <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">options</span>) {
    <span class="comment">// TODO Handle all 3 of the following scenarios</span>
    <span class="comment">// Hint: Only some of these scenarios are possible if cQuery is present</span>
    <span class="comment">// Scenario: findOne(conditions, fields, callback);</span>
    <span class="comment">// Scenario: findOne(fields, options, callback);</span>
    <span class="comment">// Scenario: findOne(conditions, options, callback);</span>
    <span class="variable">callback</span> = <span class="variable">options</span>;
    <span class="variable">options</span> = <span class="keyword">null</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">fields</span>) {
    <span class="comment">// TODO Handle all 2 of the following scenarios</span>
    <span class="comment">// Scenario: findOne(conditions, callback)</span>
    <span class="comment">// Scenario: findOne(fields, callback)</span>
    <span class="comment">// Scenario: findOne(options, callback);</span>
    <span class="variable">callback</span> = <span class="variable">fields</span>;
    <span class="variable">fields</span> = <span class="keyword">null</span>;
    <span class="variable">options</span> = <span class="keyword">null</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">conditions</span>) {
    <span class="variable">callback</span> = <span class="variable">conditions</span>;
    <span class="variable">conditions</span> = {};
    <span class="variable">fields</span> = <span class="keyword">null</span>;
    <span class="variable">options</span> = <span class="keyword">null</span>;
  }

  <span class="keyword">var</span> <span class="variable">query</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">conditions</span>, <span class="variable">options</span>).<span class="variable">select</span>(<span class="variable">fields</span>).<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'findOne'</span>);

  <span class="keyword">if</span> (<span class="string">'undefined'</span> == <span class="keyword">typeof</span> <span class="variable">callback</span>)
    <span class="keyword">return</span> <span class="variable">query</span>;

  <span class="this">this</span>.<span class="variable">_applyNamedScope</span>(<span class="variable">query</span>);
  <span class="keyword">return</span> <span class="variable">query</span>.<span class="variable">findOne</span>(<span class="variable">callback</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Counts documents</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  conditions</p></li><li><p><strong>param</strong>: <em>Function</em>  optional callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">count</span> = <span class="keyword">function</span> <span class="variable">count</span> (<span class="variable">conditions</span>, <span class="variable">callback</span>) {
  <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">conditions</span>)
    <span class="variable">callback</span> = <span class="variable">conditions</span>, <span class="variable">conditions</span> = {};

  <span class="keyword">var</span> <span class="variable">query</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">conditions</span>).<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'count'</span>);
  <span class="keyword">if</span> (<span class="string">'undefined'</span> == <span class="keyword">typeof</span> <span class="variable">callback</span>)
    <span class="keyword">return</span> <span class="variable">query</span>;

  <span class="this">this</span>.<span class="variable">_applyNamedScope</span>(<span class="variable">query</span>);
  <span class="keyword">return</span> <span class="variable">query</span>.<span class="variable">count</span>(<span class="variable">callback</span>);
};

<span class="class">Model</span>.<span class="variable">distinct</span> = <span class="keyword">function</span> <span class="variable">distinct</span> (<span class="variable">field</span>, <span class="variable">conditions</span>, <span class="variable">callback</span>) {
  <span class="keyword">var</span> <span class="variable">query</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">conditions</span>).<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'distinct'</span>);
  <span class="keyword">if</span> (<span class="string">'undefined'</span> == <span class="keyword">typeof</span> <span class="variable">callback</span>) {
    <span class="variable">query</span>.<span class="variable">_distinctArg</span> = <span class="variable">field</span>;
    <span class="keyword">return</span> <span class="variable">query</span>;
  }

  <span class="this">this</span>.<span class="variable">_applyNamedScope</span>(<span class="variable">query</span>);
  <span class="keyword">return</span> <span class="variable">query</span>.<span class="variable">distinct</span>(<span class="variable">field</span>, <span class="variable">callback</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p><code>where</code> enables a very nice sugary api for doing your queries.
For example, instead of writing:
    User.find({age: {$gte: 21, $lte: 65}}, callback);
we can instead write more readably:
    User.where('age').gte(21).lte(65);
Moreover, you can also chain a bunch of these together like:
    User
      .where('age').gte(21).lte(65)
      .where('name', /^b/i)        // All names that begin where b or B
      .where('friends').slice(10);
## </p>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>param</strong>: <em>Object</em>  val (optional)</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">where</span> = <span class="keyword">function</span> <span class="variable">where</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
  <span class="keyword">var</span> <span class="variable">q</span> = <span class="keyword">new</span> <span class="class">Query</span>().<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'find'</span>);
  <span class="keyword">return</span> <span class="variable">q</span>.<span class="variable">where</span>.<span class="variable">apply</span>(<span class="variable">q</span>, <span class="variable">arguments</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sometimes you need to query for things in mongodb using a JavaScript
expression. You can do so via find({$where: javascript}), or you can
use the mongoose shortcut method $where via a Query chain or from
your mongoose Model.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String | Function</em>  js is a javascript string or anonymous function</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.$<span class="variable">where</span> = <span class="keyword">function</span> $<span class="variable">where</span> () {
  <span class="keyword">var</span> <span class="variable">q</span> = <span class="keyword">new</span> <span class="class">Query</span>().<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'find'</span>);
  <span class="keyword">return</span> <span class="variable">q</span>.$<span class="variable">where</span>.<span class="variable">apply</span>(<span class="variable">q</span>, <span class="variable">arguments</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Shortcut for creating a new Document that is automatically saved
to the db if valid.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  doc</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">create</span> = <span class="keyword">function</span> <span class="variable">create</span> (<span class="variable">doc</span>, <span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="number integer">1</span> === <span class="variable">arguments</span>.<span class="variable">length</span>) {
    <span class="keyword">return</span> <span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">doc</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">doc</span>(<span class="keyword">null</span>);
  }

  <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>
    , <span class="variable">docs</span> = [<span class="keyword">null</span>]
    , <span class="variable">promise</span>
    , <span class="variable">count</span>
    , <span class="variable">args</span>

  <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">doc</span>)) {
    <span class="variable">args</span> = <span class="variable">doc</span>;
  } <span class="keyword">else</span> {
    <span class="variable">args</span> = <span class="variable">utils</span>.<span class="variable">args</span>(<span class="variable">arguments</span>, <span class="number integer">0</span>, <span class="variable">arguments</span>.<span class="variable">length</span> - <span class="number integer">1</span>);
    <span class="variable">fn</span> = <span class="variable">arguments</span>[<span class="variable">arguments</span>.<span class="variable">length</span> - <span class="number integer">1</span>];
  }

  <span class="keyword">if</span> (<span class="number integer">0</span> === <span class="variable">args</span>.<span class="variable">length</span>) <span class="keyword">return</span> <span class="variable">fn</span>(<span class="keyword">null</span>);

  <span class="variable">promise</span> = <span class="keyword">new</span> <span class="class">Promise</span>(<span class="variable">fn</span>);
  <span class="variable">count</span> = <span class="variable">args</span>.<span class="variable">length</span>;

  <span class="variable">args</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">arg</span>, <span class="variable">i</span>) {
    <span class="keyword">var</span> <span class="variable">doc</span> = <span class="keyword">new</span> <span class="variable">self</span>(<span class="variable">arg</span>);
    <span class="variable">docs</span>[<span class="variable">i</span>+<span class="number integer">1</span>] = <span class="variable">doc</span>;
    <span class="variable">doc</span>.<span class="variable">save</span>(<span class="keyword">function</span> (<span class="variable">err</span>) {
      <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">promise</span>.<span class="variable">error</span>(<span class="variable">err</span>);
      --<span class="variable">count</span> || <span class="variable">fn</span>.<span class="variable">apply</span>(<span class="keyword">null</span>, <span class="variable">docs</span>);
    });
  });

  <span class="comment">// TODO</span>
  <span class="comment">// utilize collection.insertAll for batch processing?</span>
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Updates documents.</p>

<h2>Examples</h2>

<pre><code>MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
MyModel.update({ name: 'Tobi' }, { ferret: true }, { multi: true }, fn);</code></pre>

<p>Valid options:</p>

<ul><li>safe (boolean) safe mode (defaults to value set in schema (true))</li><li>upsert (boolean) whether to create the doc if it doesn't match (false)</li><li>multi (boolean) whether multiple documents should be updated (false)</li></ul>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  conditions</p></li><li><p><strong>param</strong>: <em>Object</em>  doc</p></li><li><p><strong>param</strong>: <em>Object</em>  options</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">update</span> = <span class="keyword">function</span> <span class="variable">update</span> (<span class="variable">conditions</span>, <span class="variable">doc</span>, <span class="variable">options</span>, <span class="variable">callback</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> &<span class="variable">lt</span>; <span class="number integer">4</span>) {
    <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">options</span>) {
      <span class="comment">// Scenario: update(conditions, doc, callback)</span>
      <span class="variable">callback</span> = <span class="variable">options</span>;
      <span class="variable">options</span> = <span class="keyword">null</span>;
    } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">doc</span>) {
      <span class="comment">// Scenario: update(doc, callback);</span>
      <span class="variable">callback</span> = <span class="variable">doc</span>;
      <span class="variable">doc</span> = <span class="variable">conditions</span>;
      <span class="variable">conditions</span> = {};
      <span class="variable">options</span> = <span class="keyword">null</span>;
    }
  }

  <span class="keyword">var</span> <span class="variable">query</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">conditions</span>, <span class="variable">options</span>).<span class="variable">bind</span>(<span class="this">this</span>, <span class="string">'update'</span>, <span class="variable">doc</span>);

  <span class="keyword">if</span> (<span class="string">'undefined'</span> == <span class="keyword">typeof</span> <span class="variable">callback</span>)
    <span class="keyword">return</span> <span class="variable">query</span>;

  <span class="this">this</span>.<span class="variable">_applyNamedScope</span>(<span class="variable">query</span>);
  <span class="keyword">return</span> <span class="variable">query</span>.<span class="variable">update</span>(<span class="variable">doc</span>, <span class="variable">callback</span>);
};</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/namedscope.js"><a href="#">namedscope</a></h2></td><td>lib/namedscope.js</td></tr><tr class="code">
<td class="docs">
<p>var Query = require('./query');
function NamedScope () {}</p>

<p>NamedScope.prototype.query;</p>

<p>NamedScope.prototype.where = function () {
  var q = this.query || (this.query = new Query());
  q.where.apply(q, arguments);
  return q;
};</p>
</td>
<td class="code">
<pre><code>*
 * <span class="class">Decorate</span>
 *
 * @<span class="variable">param</span> {<span class="class">NamedScope</span>} <span class="variable">target</span>
 * @<span class="variable">param</span> {<span class="class">Object</span>} <span class="variable">getters</span>
 * @<span class="variable">api</span> <span class="variable">private</span>
 </code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>NamedScope.prototype.decorate = function (target, getters) {
  var name = this.name
    , block = this.block
    , query = this.query;
  if (block) {
    if (block.length === 0) {
      Object.defineProperty(target, name, {
        get: getters.block0(block)
      });
    } else {
      target[name] = getters.blockN(block);
    }
  } else {
    Object.defineProperty(target, name, {
      get: getters.basic(query)
    });
  }
};</p>

<p>NamedScope.prototype.compile = function (model) {
  var allScopes = this.scopesByName
    , scope;
  for (var k in allScopes) {
    scope = allScopes[k];
    scope.decorate(model, {
      block0: function (block) {
        return function () {
          var cquery = this.<em>cumulativeQuery || (this.</em>cumulativeQuery = new Query().bind(this));
          block.call(cquery);
          return this;
        };
      },
      blockN: function (block) {
        return function () {
          var cquery = this.<em>cumulativeQuery || (this.</em>cumulativeQuery = new Query().bind(this));
          block.apply(cquery, arguments);
          return this;
        };
      },
      basic: function (query) {
        return function () {
          var cquery = this.<em>cumulativeQuery || (this.</em>cumulativeQuery = new Query().bind(this));
          cquery.find(query);
          return this;
        };
      }
    });
  }
};</p>

<p>module.exports = NamedScope;</p>
</td>
<td class="code">

</td>
</tr><tr class="filename"><td><h2 id="lib/promise.js"><a href="#">promise</a></h2></td><td>lib/promise.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">util</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>);
<span class="keyword">var</span> <span class="class">EventEmitter</span> = <span class="variable">util</span>.<span class="class">EventEmitter</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Promise constructor.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  a callback+errback that takes err, ... as signature</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">Promise</span> (<span class="variable">back</span>) {
  <span class="this">this</span>.<span class="variable">emitted</span> = {};
  <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">back</span>)
    <span class="this">this</span>.<span class="variable">addBack</span>(<span class="variable">back</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from EventEmitter.
 </p>
</td>
<td class="code">
<pre><code><span class="class">Promise</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">EventEmitter</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds an event or fires the callback right away.</p>

<ul><li><p><strong>return</strong>: <em>promise</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Promise</span>.<span class="variable">prototype</span>.<span class="variable">on</span> = <span class="keyword">function</span> (<span class="variable">event</span>, <span class="variable">callback</span>) {
  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">emitted</span>[<span class="variable">event</span>])
    <span class="variable">callback</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="this">this</span>.<span class="variable">emitted</span>[<span class="variable">event</span>]);
  <span class="keyword">else</span>
    <span class="class">EventEmitter</span>.<span class="variable">prototype</span>.<span class="variable">on</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">event</span>, <span class="variable">callback</span>);

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Shortcut for emitting complete event</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Promise</span>.<span class="variable">prototype</span>.<span class="variable">complete</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">args</span> = <span class="variable">util</span>.<span class="variable">args</span>(<span class="variable">arguments</span>);
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">emit</span>.<span class="variable">apply</span>(<span class="this">this</span>, [<span class="string">'complete'</span>].<span class="variable">concat</span>(<span class="variable">args</span>));
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Shortcut for emitting err event</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Promise</span>.<span class="variable">prototype</span>.<span class="variable">error</span> = <span class="keyword">function</span> (<span class="variable">err</span>) {
  <span class="keyword">if</span> (!(<span class="variable">err</span> <span class="variable">instanceof</span> <span class="class">Error</span>)) <span class="variable">err</span> = <span class="keyword">new</span> <span class="class">Error</span>(<span class="variable">err</span>);
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'err'</span>, <span class="variable">err</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Shortcut for <code>.on('complete', fn)</code></p>

<ul><li><p><strong>return</strong>: <em>promise</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Promise</span>.<span class="variable">prototype</span>.<span class="variable">addCallback</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">on</span>(<span class="string">'complete'</span>, <span class="variable">fn</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Shortcut for <code>.on('err', fn)</code></p>

<ul><li><p><strong>return</strong>: <em>promise</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Promise</span>.<span class="variable">prototype</span>.<span class="variable">addErrback</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">on</span>(<span class="string">'err'</span>, <span class="variable">fn</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sugar for handling cases where you may be
resolving to either an error condition or a 
success condition.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Error</em>  optional error or null</p></li><li><p><strong>param</strong>: <em>Object</em>  value to complete the promise with</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Promise</span>.<span class="variable">prototype</span>.<span class="variable">resolve</span> = <span class="keyword">function</span> (<span class="variable">err</span>, <span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">error</span>(<span class="variable">err</span>);
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">complete</span>(<span class="variable">val</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">Promise</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/querystream.js"><a href="#">querystream</a></h2></td><td>lib/querystream.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Stream</span> = <span class="variable">require</span>(<span class="string">'stream'</span>).<span class="class">Stream</span>
<span class="keyword">var</span> <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>)</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>QueryStream</p>

<p>Returns a stream interface for the <code>query</code>.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Query</em>  query</p></li><li><p><strong>return</strong>: <em>Stream</em> </p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">QueryStream</span> (<span class="variable">query</span>) {
  <span class="class">Stream</span>.<span class="variable">call</span>(<span class="this">this</span>);

  <span class="this">this</span>.<span class="variable">query</span> = <span class="variable">query</span>;
  <span class="this">this</span>.<span class="variable">readable</span> = <span class="variable">true</span>;
  <span class="this">this</span>.<span class="variable">paused</span> = <span class="variable">false</span>;
  <span class="this">this</span>.<span class="variable">_cursor</span> = <span class="keyword">null</span>;
  <span class="this">this</span>.<span class="variable">_destroyed</span> = <span class="keyword">null</span>;
  <span class="this">this</span>.<span class="variable">_fields</span> = <span class="keyword">null</span>;
  <span class="this">this</span>.<span class="variable">_ticks</span> = <span class="number integer">0</span>;
  <span class="this">this</span>.<span class="variable">_inline</span> = <span class="class">T_INIT</span>;

  <span class="comment">// give time to hook up events</span>
  <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
  <span class="variable">process</span>.<span class="variable">nextTick</span>(<span class="keyword">function</span> () {
    <span class="variable">self</span>.<span class="variable">_init</span>();
  });
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Flag stating whether or not this stream is readable.
 </p>
</td>
<td class="code">
<pre><code><span class="class">QueryStream</span>.<span class="variable">prototype</span>.<span class="variable">readable</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Flag stating whether or not this stream is paused.
 </p>
</td>
<td class="code">
<pre><code><span class="class">QueryStream</span>.<span class="variable">prototype</span>.<span class="variable">paused</span>;

<span class="comment">// trampoline flags</span>
<span class="keyword">var</span> <span class="class">T_INIT</span> = <span class="number integer">0</span>;
<span class="keyword">var</span> <span class="class">T_IDLE</span> = <span class="number integer">1</span>;
<span class="keyword">var</span> <span class="class">T_CONT</span> = <span class="number integer">2</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pauses this stream.
 </p>
</td>
<td class="code">
<pre><code><span class="class">QueryStream</span>.<span class="variable">prototype</span>.<span class="variable">pause</span> = <span class="keyword">function</span> () {
  <span class="this">this</span>.<span class="variable">paused</span> = <span class="variable">true</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Resumes this stream.
 </p>
</td>
<td class="code">
<pre><code><span class="class">QueryStream</span>.<span class="variable">prototype</span>.<span class="variable">resume</span> = <span class="keyword">function</span> () {
  <span class="this">this</span>.<span class="variable">paused</span> = <span class="variable">false</span>;
  <span class="this">this</span>.<span class="variable">_next</span>();
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Destroys the stream, closing the underlying
cursor. No more events will be emitted.
 </p>
</td>
<td class="code">
<pre><code><span class="class">QueryStream</span>.<span class="variable">prototype</span>.<span class="variable">destroy</span> = <span class="keyword">function</span> (<span class="variable">err</span>) {
  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">_destroyed</span>) <span class="keyword">return</span>;
  <span class="this">this</span>.<span class="variable">_destroyed</span> = <span class="variable">true</span>;
  <span class="this">this</span>.<span class="variable">readable</span> = <span class="variable">false</span>;

  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">_cursor</span>) {
    <span class="this">this</span>.<span class="variable">_cursor</span>.<span class="variable">close</span>();
  }

  <span class="keyword">if</span> (<span class="variable">err</span>) {
    <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'error'</span>, <span class="variable">err</span>);
  }

  <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'close'</span>);
}

<span class="comment">// TODO - maybe implement the -native raw option to pass binary?</span>
<span class="comment">//QueryStream.prototype.setEncoding = function () {</span>
<span class="comment">//}</span>

<span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">exports</span> = <span class="class">QueryStream</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/query.js"><a href="#">query</a></h2></td><td>lib/query.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>)
  , <span class="variable">merge</span> = <span class="variable">utils</span>.<span class="variable">merge</span>
  , <span class="class">Promise</span> = <span class="variable">require</span>(<span class="string">'./promise'</span>)
  , <span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'./document'</span>)
  , <span class="variable">inGroupsOf</span> = <span class="variable">utils</span>.<span class="variable">inGroupsOf</span>
  , <span class="variable">tick</span> = <span class="variable">utils</span>.<span class="variable">tick</span>
  , <span class="class">QueryStream</span> = <span class="variable">require</span>(<span class="string">'./querystream'</span>)</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Binds this query to a model.
## </p>

<ul><li><p><strong>param</strong>: <em>Function</em>  param</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">bind</span> = <span class="keyword">function</span> <span class="variable">bind</span> (<span class="variable">model</span>, <span class="variable">op</span>, <span class="variable">updateArg</span>) {
  <span class="this">this</span>.<span class="variable">model</span> = <span class="variable">model</span>;
  <span class="this">this</span>.<span class="variable">op</span> = <span class="variable">op</span>;
  <span class="keyword">if</span> (<span class="variable">op</span> === <span class="string">'update'</span>) <span class="this">this</span>.<span class="variable">_updateArg</span> = <span class="variable">updateArg</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Executes the query returning a promise.</p>

<h2>Examples</h2>

<p>query.exec();
query.exec(callback);
query.exec('update');
query.exec('find', callback);</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String | Function</em>  op (optional)</p></li><li><p><strong>param</strong>: <em>Function</em>  callback (optional)</p></li><li><p><strong>return</strong>: <em>Promise</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">exec</span> = <span class="keyword">function</span> (<span class="variable">op</span>, <span class="variable">callback</span>) {
  <span class="keyword">var</span> <span class="variable">promise</span> = <span class="keyword">new</span> <span class="class">Promise</span>();

  <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">op</span>) {
    <span class="keyword">case</span> <span class="string">'function'</span>:
      <span class="variable">callback</span> = <span class="variable">op</span>;
      <span class="variable">op</span> = <span class="keyword">null</span>;
      <span class="keyword">break</span>;
    <span class="keyword">case</span> <span class="string">'string'</span>:
      <span class="this">this</span>.<span class="variable">op</span> = <span class="variable">op</span>;
      <span class="keyword">break</span>;
  }

  <span class="keyword">if</span> (<span class="variable">callback</span>) <span class="variable">promise</span>.<span class="variable">addBack</span>(<span class="variable">callback</span>);

  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">op</span>) {
    <span class="variable">promise</span>.<span class="variable">complete</span>();
    <span class="keyword">return</span> <span class="variable">promise</span>;
  }

  <span class="keyword">if</span> (<span class="string">'update'</span> == <span class="this">this</span>.<span class="variable">op</span>) {
    <span class="this">this</span>.<span class="variable">update</span>(<span class="this">this</span>.<span class="variable">_updateArg</span>, <span class="variable">promise</span>.<span class="variable">resolve</span>.<span class="variable">bind</span>(<span class="variable">promise</span>));
    <span class="keyword">return</span> <span class="variable">promise</span>;
  }

  <span class="keyword">if</span> (<span class="string">'distinct'</span> == <span class="this">this</span>.<span class="variable">op</span>) {
    <span class="this">this</span>.<span class="variable">distinct</span>(<span class="this">this</span>.<span class="variable">_distinctArg</span>, <span class="variable">promise</span>.<span class="variable">resolve</span>.<span class="variable">bind</span>(<span class="variable">promise</span>));
    <span class="keyword">return</span> <span class="variable">promise</span>;
  }

  <span class="this">this</span>[<span class="this">this</span>.<span class="variable">op</span>](<span class="variable">promise</span>.<span class="variable">resolve</span>.<span class="variable">bind</span>(<span class="variable">promise</span>));
  <span class="keyword">return</span> <span class="variable">promise</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Finds documents.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  criteria</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">find</span> = <span class="keyword">function</span> (<span class="variable">criteria</span>, <span class="variable">callback</span>) {
  <span class="this">this</span>.<span class="variable">op</span> = <span class="string">'find'</span>;
  <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">criteria</span>) {
    <span class="variable">callback</span> = <span class="variable">criteria</span>;
    <span class="variable">criteria</span> = {};
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">criteria</span> <span class="variable">instanceof</span> <span class="class">Query</span>) {
    <span class="comment">// TODO Merge options, too</span>
    <span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">_conditions</span>, <span class="variable">criteria</span>.<span class="variable">_conditions</span>);
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">criteria</span> <span class="variable">instanceof</span> <span class="class">Document</span>) {
    <span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">_conditions</span>, <span class="variable">criteria</span>.<span class="variable">toObject</span>());
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">criteria</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'Object'</span> === <span class="variable">criteria</span>.<span class="variable">constructor</span>.<span class="variable">name</span>) {
    <span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">_conditions</span>, <span class="variable">criteria</span>);
  }
  <span class="keyword">if</span> (!<span class="variable">callback</span>) <span class="keyword">return</span> <span class="this">this</span>;
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">execFind</span>(<span class="variable">callback</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Casts obj, or if obj is not present, then this._conditions,
based on the model's schema.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  model</p></li><li><p><strong>param</strong>: <em>Object</em>  obj (optional)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">cast</span> = <span class="keyword">function</span> (<span class="variable">model</span>, <span class="variable">obj</span>) {
  <span class="variable">obj</span> || (<span class="variable">obj</span>= <span class="this">this</span>.<span class="variable">_conditions</span>);

  <span class="keyword">var</span> <span class="variable">schema</span> = <span class="variable">model</span>.<span class="variable">schema</span>
    , <span class="variable">paths</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">obj</span>)
    , <span class="variable">i</span> = <span class="variable">paths</span>.<span class="variable">length</span>
    , <span class="variable">any</span>$<span class="variable">conditionals</span>
    , <span class="variable">schematype</span>
    , <span class="variable">nested</span>
    , <span class="variable">path</span>
    , <span class="variable">type</span>
    , <span class="variable">val</span>;

  <span class="keyword">while</span> (<span class="variable">i</span>--) {
    <span class="variable">path</span> = <span class="variable">paths</span>[<span class="variable">i</span>];
    <span class="variable">val</span> = <span class="variable">obj</span>[<span class="variable">path</span>];

    <span class="keyword">if</span> (<span class="string">'$or'</span> === <span class="variable">path</span> || <span class="string">'$nor'</span> === <span class="variable">path</span>) {
      <span class="keyword">var</span> <span class="variable">k</span> = <span class="variable">val</span>.<span class="variable">length</span>
        , <span class="variable">orComponentQuery</span>;

      <span class="keyword">while</span> (<span class="variable">k</span>--) {
        <span class="variable">orComponentQuery</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">val</span>[<span class="variable">k</span>]);
        <span class="variable">orComponentQuery</span>.<span class="variable">cast</span>(<span class="variable">model</span>);
        <span class="variable">val</span>[<span class="variable">k</span>] = <span class="variable">orComponentQuery</span>.<span class="variable">_conditions</span>;
      }

    } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">path</span> === <span class="string">'$where'</span>) {
      <span class="variable">type</span> = <span class="keyword">typeof</span> <span class="variable">val</span>;

      <span class="keyword">if</span> (<span class="string">'string'</span> !== <span class="variable">type</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'function'</span> !== <span class="variable">type</span>) {
        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(&<span class="variable">quot</span>;<span class="class">Must</span> <span class="variable">have</span> <span class="variable">a</span> <span class="variable">string</span> <span class="variable">or</span> <span class="keyword">function</span> <span class="keyword">for</span> $<span class="variable">where</span>&<span class="variable">quot</span>;);
      }

      <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="variable">type</span>) {
        <span class="variable">obj</span>[<span class="variable">path</span>] = <span class="variable">val</span>.<span class="variable">toString</span>();
      }

      <span class="keyword">continue</span>;

    } <span class="keyword">else</span> {

      <span class="keyword">if</span> (!<span class="variable">schema</span>) {
        <span class="comment">// no casting for Mixed types</span>
        <span class="keyword">continue</span>;
      }

      <span class="variable">schematype</span> = <span class="variable">schema</span>.<span class="variable">path</span>(<span class="variable">path</span>);

      <span class="keyword">if</span> (!<span class="variable">schematype</span>) {
        <span class="comment">// Handle potential embedded array queries</span>
        <span class="keyword">var</span> <span class="variable">split</span> = <span class="variable">path</span>.<span class="variable">split</span>(<span class="string">'.'</span>)
          , <span class="variable">j</span> = <span class="variable">split</span>.<span class="variable">length</span>
          , <span class="variable">pathFirstHalf</span>
          , <span class="variable">pathLastHalf</span>
          , <span class="variable">remainingConds</span>
          , <span class="variable">castingQuery</span>;

        <span class="comment">// Find the part of the var path that is a path of the Schema</span>
        <span class="keyword">while</span> (<span class="variable">j</span>--) {
          <span class="variable">pathFirstHalf</span> = <span class="variable">split</span>.<span class="variable">slice</span>(<span class="number integer">0</span>, <span class="variable">j</span>).<span class="variable">join</span>(<span class="string">'.'</span>);
          <span class="variable">schematype</span> = <span class="variable">schema</span>.<span class="variable">path</span>(<span class="variable">pathFirstHalf</span>);
          <span class="keyword">if</span> (<span class="variable">schematype</span>) <span class="keyword">break</span>;
        }

        <span class="comment">// If a substring of the input path resolves to an actual real path...</span>
        <span class="keyword">if</span> (<span class="variable">schematype</span>) {
          <span class="comment">// Apply the casting; similar code for $elemMatch in schema/array.js</span>
          <span class="keyword">if</span> (<span class="variable">schematype</span>.<span class="variable">caster</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">schematype</span>.<span class="variable">caster</span>.<span class="variable">schema</span>) {
            <span class="variable">remainingConds</span> = {};
            <span class="variable">pathLastHalf</span> = <span class="variable">split</span>.<span class="variable">slice</span>(<span class="variable">j</span>).<span class="variable">join</span>(<span class="string">'.'</span>);
            <span class="variable">remainingConds</span>[<span class="variable">pathLastHalf</span>] = <span class="variable">val</span>;
            <span class="variable">castingQuery</span> = <span class="keyword">new</span> <span class="class">Query</span>(<span class="variable">remainingConds</span>);
            <span class="variable">castingQuery</span>.<span class="variable">cast</span>(<span class="variable">schematype</span>.<span class="variable">caster</span>);
            <span class="variable">obj</span>[<span class="variable">path</span>] = <span class="variable">castingQuery</span>.<span class="variable">_conditions</span>[<span class="variable">pathLastHalf</span>];
          } <span class="keyword">else</span> {
            <span class="variable">obj</span>[<span class="variable">path</span>] = <span class="variable">val</span>;
          }
        }

      } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">val</span> === <span class="keyword">null</span> || <span class="variable">val</span> === <span class="variable">undefined</span>) {
        <span class="keyword">continue</span>;
      } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'Object'</span> === <span class="variable">val</span>.<span class="variable">constructor</span>.<span class="variable">name</span>) {

        <span class="variable">any</span>$<span class="variable">conditionals</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">val</span>).<span class="variable">some</span>(<span class="keyword">function</span> (<span class="variable">k</span>) {
          <span class="keyword">return</span> <span class="variable">k</span>.<span class="variable">charAt</span>(<span class="number integer">0</span>) === <span class="string">'$'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">k</span> !== <span class="string">'$id'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">k</span> !== <span class="string">'$ref'</span>;
        });

        <span class="keyword">if</span> (!<span class="variable">any</span>$<span class="variable">conditionals</span>) {
          <span class="variable">obj</span>[<span class="variable">path</span>] = <span class="variable">schematype</span>.<span class="variable">castForQuery</span>(<span class="variable">val</span>);
        } <span class="keyword">else</span> {

          <span class="keyword">var</span> <span class="variable">ks</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">val</span>)
            , <span class="variable">k</span> = <span class="variable">ks</span>.<span class="variable">length</span>
            , $<span class="variable">cond</span>;

          <span class="keyword">while</span> (<span class="variable">k</span>--) {
            $<span class="variable">cond</span> = <span class="variable">ks</span>[<span class="variable">k</span>];
            <span class="variable">nested</span> = <span class="variable">val</span>[$<span class="variable">cond</span>];

            <span class="keyword">if</span> (<span class="string">'$exists'</span> === $<span class="variable">cond</span>) {
              <span class="keyword">if</span> (<span class="string">'boolean'</span> !== <span class="keyword">typeof</span> <span class="variable">nested</span>) {
                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(&<span class="variable">quot</span>;$<span class="variable">exists</span> <span class="variable">parameter</span> <span class="variable">must</span> <span class="variable">be</span> <span class="class">Boolean</span>&<span class="variable">quot</span>;);
              }
              <span class="keyword">continue</span>;
            }

            <span class="keyword">if</span> (<span class="string">'$type'</span> === $<span class="variable">cond</span>) {
              <span class="keyword">if</span> (<span class="string">'number'</span> !== <span class="keyword">typeof</span> <span class="variable">nested</span>) {
                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(&<span class="variable">quot</span>;$<span class="variable">type</span> <span class="variable">parameter</span> <span class="variable">must</span> <span class="variable">be</span> <span class="class">Number</span>&<span class="variable">quot</span>;);
              }
              <span class="keyword">continue</span>;
            }

            <span class="keyword">if</span> (<span class="string">'$not'</span> === $<span class="variable">cond</span>) {
              <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">model</span>, <span class="variable">nested</span>);
            } <span class="keyword">else</span> {
              <span class="variable">val</span>[$<span class="variable">cond</span>] = <span class="variable">schematype</span>.<span class="variable">castForQuery</span>($<span class="variable">cond</span>, <span class="variable">nested</span>);
            }
          }
        }
      } <span class="keyword">else</span> {
        <span class="variable">obj</span>[<span class="variable">path</span>] = <span class="variable">schematype</span>.<span class="variable">castForQuery</span>(<span class="variable">val</span>);
      }
    }
  }
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sometimes you need to query for things in mongodb using a JavaScript
expression. You can do so via find({$where: javascript}), or you can
use the mongoose shortcut method $where via a Query chain or from
your mongoose Model.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String | Function</em>  js is a javascript string or anonymous function</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">where</span> = <span class="keyword">function</span> (<span class="variable">js</span>) {
  <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="string">'$where'</span>] = <span class="variable">js</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p><code>where</code> enables a very nice sugary api for doing your queries.
For example, instead of writing:</p>

<pre><code>User.find({age: {$gte: 21, $lte: 65}}, callback);</code></pre>

<p>we can instead write more readably:</p>

<pre><code>User.where('age').gte(21).lte(65);</code></pre>

<p>Moreover, you can also chain a bunch of these together like:</p>

<pre><code>User
  .where('age').gte(21).lte(65)
  .where('name', /^b/i)        // All names that begin where b or B
  .where('friends').slice(10);</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>param</strong>: <em>Object</em>  val (optional)</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">where</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="number integer">2</span> === <span class="variable">arguments</span>.<span class="variable">length</span>) {
    <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = <span class="variable">val</span>;
  }
  <span class="this">this</span>.<span class="variable">_currPath</span> = <span class="variable">path</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p><code>equals</code> sugar.</p>

<pre><code>User.where('age').equals(49);</code></pre>

<p>Same as</p>

<pre><code>User.where('age', 49);</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>object</em>  val</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">equals</span> = <span class="keyword">function</span> <span class="variable">equals</span> (<span class="variable">val</span>) {
  <span class="keyword">var</span> <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
  <span class="keyword">if</span> (!<span class="variable">path</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'equals() must be used after where()'</span>);
  <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = <span class="variable">val</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>or
 </p>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">or</span> = <span class="keyword">function</span> <span class="variable">or</span> (<span class="variable">array</span>) {
  <span class="keyword">var</span> <span class="variable">or</span> = <span class="this">this</span>.<span class="variable">_conditions</span>.$<span class="variable">or</span> || (<span class="this">this</span>.<span class="variable">_conditions</span>.$<span class="variable">or</span> = []);
  <span class="keyword">if</span> (!<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">array</span>)) <span class="variable">array</span> = [<span class="variable">array</span>];
  <span class="variable">or</span>.<span class="variable">push</span>.<span class="variable">apply</span>(<span class="variable">or</span>, <span class="variable">array</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>nor
 </p>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">nor</span> = <span class="keyword">function</span> <span class="variable">nor</span> (<span class="variable">array</span>) {
  <span class="keyword">var</span> <span class="variable">nor</span> = <span class="this">this</span>.<span class="variable">_conditions</span>.$<span class="variable">nor</span> || (<span class="this">this</span>.<span class="variable">_conditions</span>.$<span class="variable">nor</span> = []);
  <span class="keyword">if</span> (!<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">array</span>)) <span class="variable">array</span> = [<span class="variable">array</span>];
  <span class="variable">nor</span>.<span class="variable">push</span>.<span class="variable">apply</span>(<span class="variable">nor</span>, <span class="variable">array</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>gt, gte, lt, lte, ne, in, nin, all, regex, size, maxDistance</p>

<p>Can be used on Numbers or Dates.</p>

<pre><code>Thing.where('type').nin(array)</code></pre>

<p> </p>
</td>
<td class="code">
<pre><code><span class="string">'gt gte lt lte ne in nin all regex size maxDistance'</span>.<span class="variable">split</span>(<span class="string">' '</span>).<span class="variable">forEach</span>(<span class="keyword">function</span> ($<span class="variable">conditional</span>) {
  <span class="class">Query</span>.<span class="variable">prototype</span>[$<span class="variable">conditional</span>] = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
    <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span>) {
      <span class="variable">val</span> = <span class="variable">path</span>;
      <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>
    }
    <span class="keyword">var</span> <span class="variable">conds</span> = <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] || (<span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = {});
    <span class="variable">conds</span>[<span class="string">'$'</span> + $<span class="variable">conditional</span>] = <span class="variable">val</span>;
    <span class="keyword">return</span> <span class="this">this</span>;
  };

  <span class="comment">// deprecationed</span>
  <span class="class">Query</span>.<span class="variable">prototype</span>[<span class="string">'$'</span> + $<span class="variable">conditional</span>] = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$'</span>+ $<span class="variable">conditional</span>, <span class="string">'Query#'</span> + $<span class="variable">conditional</span>, <span class="class">Query</span>.<span class="variable">prototype</span>[$<span class="variable">conditional</span>]);
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>mod, near
 </p>
</td>
<td class="code">
<pre><code>;[<span class="string">'mod'</span>, <span class="string">'near'</span>].<span class="variable">forEach</span>( <span class="keyword">function</span> ($<span class="variable">conditional</span>) {
  <span class="class">Query</span>.<span class="variable">prototype</span>[$<span class="variable">conditional</span>] = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
    <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span>) {
      <span class="variable">val</span> = <span class="variable">path</span>;
      <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>
    } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">2</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">val</span>)) {
      <span class="variable">val</span> = <span class="variable">utils</span>.<span class="variable">args</span>(<span class="variable">arguments</span>);
      <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
    } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">3</span>) {
      <span class="variable">val</span> = <span class="variable">utils</span>.<span class="variable">args</span>(<span class="variable">arguments</span>, <span class="number integer">1</span>);
    }
    <span class="keyword">var</span> <span class="variable">conds</span> = <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] || (<span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = {});
    <span class="variable">conds</span>[<span class="string">'$'</span> + $<span class="variable">conditional</span>] = <span class="variable">val</span>;
    <span class="keyword">return</span> <span class="this">this</span>;
  };
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>exists
 </p>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">exists</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">0</span>) {
    <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>
    <span class="variable">val</span> = <span class="variable">true</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span>) {
    <span class="keyword">if</span> (<span class="string">'boolean'</span> === <span class="keyword">typeof</span> <span class="variable">path</span>) {
      <span class="variable">val</span> = <span class="variable">path</span>;
      <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
    } <span class="keyword">else</span> {
      <span class="variable">val</span> = <span class="variable">true</span>;
    }
  }
  <span class="keyword">var</span> <span class="variable">conds</span> = <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] || (<span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = {});
  <span class="variable">conds</span>[<span class="string">'$exists'</span>] = <span class="variable">val</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>elemMatch
 </p>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">elemMatch</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">criteria</span>) {
  <span class="keyword">var</span> <span class="variable">block</span>;
  <span class="keyword">if</span> (<span class="string">'Object'</span> === <span class="variable">path</span>.<span class="variable">constructor</span>.<span class="variable">name</span>) {
    <span class="variable">criteria</span> = <span class="variable">path</span>;
    <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">path</span>) {
    <span class="variable">block</span> = <span class="variable">path</span>;
    <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'Object'</span> === <span class="variable">criteria</span>.<span class="variable">constructor</span>.<span class="variable">name</span>) {
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> <span class="variable">criteria</span>) {
    <span class="variable">block</span> = <span class="variable">criteria</span>;
  } <span class="keyword">else</span> {
    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(&<span class="variable">quot</span>;<span class="class">Argument</span> <span class="variable">error</span>&<span class="variable">quot</span>;);
  }
  <span class="keyword">var</span> <span class="variable">conds</span> = <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] || (<span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = {});
  <span class="keyword">if</span> (<span class="variable">block</span>) {
    <span class="variable">criteria</span> = <span class="keyword">new</span> <span class="class">Query</span>();
    <span class="variable">block</span>(<span class="variable">criteria</span>);
    <span class="variable">conds</span>[<span class="string">'$elemMatch'</span>] = <span class="variable">criteria</span>.<span class="variable">_conditions</span>;
  } <span class="keyword">else</span> {
    <span class="variable">conds</span>[<span class="string">'$elemMatch'</span>] = <span class="variable">criteria</span>;
  }
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Spatial queries
 </p>
</td>
<td class="code">
<pre><code><span class="class">Object</span>.<span class="variable">defineProperty</span>(<span class="class">Query</span>.<span class="variable">prototype</span>, <span class="string">'within'</span>, {
  <span class="variable">get</span>: <span class="keyword">function</span> () { <span class="keyword">return</span> <span class="this">this</span> }
});

<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">box</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span>) {
    <span class="variable">val</span> = <span class="variable">path</span>;
    <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
  }
  <span class="keyword">var</span> <span class="variable">conds</span> = <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] || (<span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = {});
  <span class="variable">conds</span>[<span class="string">'$within'</span>] = { <span class="string">'$box'</span>: [<span class="variable">val</span>.<span class="variable">ll</span>, <span class="variable">val</span>.<span class="variable">ur</span>]  };
  <span class="keyword">return</span> <span class="this">this</span>;
};

<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">center</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span>) {
    <span class="variable">val</span> = <span class="variable">path</span>;
    <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
  }
  <span class="keyword">var</span> <span class="variable">conds</span> = <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] || (<span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = {});
  <span class="variable">conds</span>[<span class="string">'$within'</span>] = { <span class="string">'$center'</span>: [<span class="variable">val</span>.<span class="variable">center</span>, <span class="variable">val</span>.<span class="variable">radius</span>]  };
  <span class="keyword">return</span> <span class="this">this</span>;
};

<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">centerSphere</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span>) {
    <span class="variable">val</span> = <span class="variable">path</span>;
    <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
  }
  <span class="keyword">var</span> <span class="variable">conds</span> = <span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] || (<span class="this">this</span>.<span class="variable">_conditions</span>[<span class="variable">path</span>] = {});
  <span class="variable">conds</span>[<span class="string">'$within'</span>] = { <span class="string">'$centerSphere'</span>: [<span class="variable">val</span>.<span class="variable">center</span>, <span class="variable">val</span>.<span class="variable">radius</span>]  };
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>select</p>

<p>Chainable method for specifying which fields
to include or exclude from the document that is
returned from MongoDB.</p>

<h2>Examples</h2>

<pre><code>query.fields({a: 1, b: 1, c: 1, _id: 0});
query.fields('a b c');</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em> </p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">select</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">arg0</span> = <span class="variable">arguments</span>[<span class="number integer">0</span>];
  <span class="keyword">if</span> (!<span class="variable">arg0</span>) <span class="keyword">return</span> <span class="this">this</span>;
  <span class="keyword">if</span> (<span class="string">'Object'</span> === <span class="variable">arg0</span>.<span class="variable">constructor</span>.<span class="variable">name</span> || <span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">arg0</span>)) {
    <span class="this">this</span>.<span class="variable">_applyFields</span>(<span class="variable">arg0</span>);
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="keyword">typeof</span> <span class="variable">arg0</span> === <span class="string">'string'</span>) {
    <span class="this">this</span>.<span class="variable">_applyFields</span>({<span class="variable">only</span>: <span class="variable">arg0</span>});
  } <span class="keyword">else</span> {
    <span class="this">this</span>.<span class="variable">_applyFields</span>({<span class="variable">only</span>: <span class="this">this</span>.<span class="variable">_parseOnlyExcludeFields</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>)});
  }
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>slice()
 </p>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">slice</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">1</span>) {
      <span class="variable">val</span> = <span class="variable">path</span>;
      <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">2</span>) {
    <span class="keyword">if</span> (<span class="string">'number'</span> === <span class="keyword">typeof</span> <span class="variable">path</span>) {
      <span class="variable">val</span> = [<span class="variable">path</span>, <span class="variable">val</span>];
      <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">_currPath</span>;
    }
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> === <span class="number integer">3</span>) {
    <span class="variable">val</span> = <span class="variable">utils</span>.<span class="variable">args</span>(<span class="variable">arguments</span>, <span class="number integer">1</span>);
  }
  <span class="keyword">var</span> <span class="variable">myFields</span> = <span class="this">this</span>.<span class="variable">_fields</span> || (<span class="this">this</span>.<span class="variable">_fields</span> = {});
  <span class="variable">myFields</span>[<span class="variable">path</span>] = { <span class="string">'$slice'</span>: <span class="variable">val</span> };
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>sort</p>

<p>Sets the sort</p>

<h2>Examples</h2>

<pre><code>query.sort('test', 1)
query.sort('field', -1)
query.sort('field', -1, 'test', 1)</code></pre>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">sort</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">sort</span> = <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">sort</span> || (<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">sort</span> = []);

  <span class="variable">inGroupsOf</span>(<span class="number integer">2</span>, <span class="variable">arguments</span>, <span class="keyword">function</span> (<span class="variable">field</span>, <span class="variable">value</span>) {
    <span class="variable">sort</span>.<span class="variable">push</span>([<span class="variable">field</span>, <span class="variable">value</span>]);
  });

  <span class="keyword">return</span> <span class="this">this</span>;
};

;[<span class="string">'limit'</span>, <span class="string">'skip'</span>, <span class="string">'maxscan'</span>, <span class="string">'snapshot'</span>, <span class="string">'batchSize'</span>, <span class="string">'comment'</span>].<span class="variable">forEach</span>( <span class="keyword">function</span> (<span class="variable">method</span>) {
  <span class="class">Query</span>.<span class="variable">prototype</span>[<span class="variable">method</span>] = <span class="keyword">function</span> (<span class="variable">v</span>) {
    <span class="this">this</span>.<span class="variable">options</span>[<span class="variable">method</span>] = <span class="variable">v</span>;
    <span class="keyword">return</span> <span class="this">this</span>;
  };
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>hint</p>

<p>Sets query hints.</p>

<h2>Examples</h2>

<pre><code>new Query().hint({ indexA: 1, indexB: -1})
new Query().hint("indexA", 1, "indexB", -1)</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object | String</em>  v</p></li><li><p><strong>param</strong>: <em>Int</em>  [multi]</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">hint</span> = <span class="keyword">function</span> (<span class="variable">v</span>, <span class="variable">multi</span>) {
  <span class="keyword">var</span> <span class="variable">hint</span> = <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">hint</span> || (<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">hint</span> = {})
    , <span class="variable">k</span>

  <span class="keyword">if</span> (<span class="variable">multi</span>) {
    <span class="variable">inGroupsOf</span>(<span class="number integer">2</span>, <span class="variable">arguments</span>, <span class="keyword">function</span> (<span class="variable">field</span>, <span class="variable">val</span>) {
      <span class="variable">hint</span>[<span class="variable">field</span>] = <span class="variable">val</span>;
    });
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'Object'</span> === <span class="variable">v</span>.<span class="variable">constructor</span>.<span class="variable">name</span>) {
    <span class="comment">// must keep object keys in order so don't use Object.keys()</span>
    <span class="keyword">for</span> (<span class="variable">k</span> <span class="keyword">in</span> <span class="variable">v</span>) {
      <span class="variable">hint</span>[<span class="variable">k</span>] = <span class="variable">v</span>[<span class="variable">k</span>];
    }
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>slaveOk</p>

<p>Sets slaveOk option.</p>

<pre><code>new Query().slaveOk() &lt;== true
new Query().slaveOk(true)
new Query().slaveOk(false)</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Boolean</em>  v (defaults to true)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">slaveOk</span> = <span class="keyword">function</span> (<span class="variable">v</span>) {
  <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">slaveOk</span> = <span class="variable">arguments</span>.<span class="variable">length</span> ? !!<span class="variable">v</span> : <span class="variable">true</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>tailable</p>

<p>Sets tailable option.</p>

<pre><code>new Query().tailable() &lt;== true
new Query().tailable(true)
new Query().tailable(false)</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Boolean</em>  v (defaults to true)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">tailable</span> = <span class="keyword">function</span> (<span class="variable">v</span>) {
  <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">tailable</span> = <span class="variable">arguments</span>.<span class="variable">length</span> ? !!<span class="variable">v</span> : <span class="variable">true</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>findOne</p>

<p>Casts the query, sends the findOne command to mongodb.
Upon receiving the document, we initialize a mongoose
document based on the returned document from mongodb,
and then we invoke a callback on our mongoose document.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  callback function (err, found)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">findOne</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
  <span class="this">this</span>.<span class="variable">op</span> = <span class="string">'findOne'</span>;

  <span class="keyword">if</span> (!<span class="variable">callback</span>) <span class="keyword">return</span> <span class="this">this</span>;

  <span class="keyword">var</span> <span class="variable">model</span> = <span class="this">this</span>.<span class="variable">model</span>;
  <span class="keyword">var</span> <span class="variable">promise</span> = <span class="keyword">new</span> <span class="class">Promise</span>(<span class="variable">callback</span>);

  <span class="keyword">try</span> {
    <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">model</span>);
  } <span class="keyword">catch</span> (<span class="variable">err</span>) {
    <span class="variable">promise</span>.<span class="variable">error</span>(<span class="variable">err</span>);
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="comment">// apply default schematype path selections</span>
  <span class="this">this</span>.<span class="variable">_applyPaths</span>();

  <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>
    , <span class="variable">castQuery</span> = <span class="this">this</span>.<span class="variable">_conditions</span>
    , <span class="variable">options</span> = <span class="this">this</span>.<span class="variable">_optionsForExec</span>(<span class="variable">model</span>)

  <span class="keyword">var</span> <span class="variable">fields</span> = <span class="variable">utils</span>.<span class="variable">clone</span>(<span class="variable">options</span>.<span class="variable">fields</span> = <span class="this">this</span>.<span class="variable">_fields</span>);

  <span class="variable">model</span>.<span class="variable">collection</span>.<span class="variable">findOne</span>(<span class="variable">castQuery</span>, <span class="variable">options</span>, <span class="variable">tick</span>(<span class="keyword">function</span> (<span class="variable">err</span>, <span class="variable">doc</span>) {
    <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">promise</span>.<span class="variable">error</span>(<span class="variable">err</span>);
    <span class="keyword">if</span> (!<span class="variable">doc</span>) <span class="keyword">return</span> <span class="variable">promise</span>.<span class="variable">complete</span>(<span class="keyword">null</span>);

    <span class="keyword">var</span> <span class="variable">casted</span> = <span class="keyword">new</span> <span class="variable">model</span>(<span class="variable">undefined</span>, <span class="variable">fields</span>);

    <span class="comment">// skip _id for pre-init hooks</span>
    <span class="keyword">delete</span> <span class="variable">casted</span>.<span class="variable">_doc</span>.<span class="variable">_id</span>;

    <span class="variable">casted</span>.<span class="variable">init</span>(<span class="variable">doc</span>, <span class="variable">self</span>, <span class="keyword">function</span> (<span class="variable">err</span>) {
      <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">promise</span>.<span class="variable">error</span>(<span class="variable">err</span>);
      <span class="variable">promise</span>.<span class="variable">complete</span>(<span class="variable">casted</span>);
    });
  }));

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>count</p>

<p>Casts this._conditions and sends a count
command to mongodb. Invokes a callback upon
receiving results</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  callback fn(err, cardinality)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">count</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
  <span class="this">this</span>.<span class="variable">op</span> = <span class="string">'count'</span>;
  <span class="keyword">var</span> <span class="variable">model</span> = <span class="this">this</span>.<span class="variable">model</span>;

  <span class="keyword">try</span> {
    <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">model</span>);
  } <span class="keyword">catch</span> (<span class="variable">err</span>) {
    <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>);
  }

  <span class="keyword">var</span> <span class="variable">castQuery</span> = <span class="this">this</span>.<span class="variable">_conditions</span>;
  <span class="variable">model</span>.<span class="variable">collection</span>.<span class="variable">count</span>(<span class="variable">castQuery</span>, <span class="variable">tick</span>(<span class="variable">callback</span>));

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>distinct</p>

<p>Casts this._conditions and sends a distinct
command to mongodb. Invokes a callback upon
receiving results</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  callback fn(err, cardinality)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">distinct</span> = <span class="keyword">function</span> (<span class="variable">field</span>, <span class="variable">callback</span>) {
  <span class="this">this</span>.<span class="variable">op</span> = <span class="string">'distinct'</span>;
  <span class="keyword">var</span> <span class="variable">model</span> = <span class="this">this</span>.<span class="variable">model</span>;

  <span class="keyword">try</span> {
    <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">model</span>);
  } <span class="keyword">catch</span> (<span class="variable">err</span>) {
    <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>);
  }

  <span class="keyword">var</span> <span class="variable">castQuery</span> = <span class="this">this</span>.<span class="variable">_conditions</span>;
  <span class="variable">model</span>.<span class="variable">collection</span>.<span class="variable">distinct</span>(<span class="variable">field</span>, <span class="variable">castQuery</span>, <span class="variable">tick</span>(<span class="variable">callback</span>));

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>update</p>

<p>Casts the <code>doc</code> according to the model Schema and
sends an update command to MongoDB.</p>

<p><em>All paths passed that are not $atomic operations
will become $set ops so we retain backwards compatibility.</em></p>

<h2>Example</h2>

<p><code>Model.update({..}, { title: 'remove words' }, ...)</code></p>

<p>  becomes</p>

<p><code>Model.update({..}, { $set: { title: 'remove words' }}, ...)</code></p>

<p><em>Passing an empty object <code>{}</code> as the doc will result
in a no-op. The update operation will be ignored and the
callback executed without sending the command to MongoDB so as
to prevent accidently overwritting the collection.</em></p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  doc - the update</p></li><li><p><strong>param</strong>: <em>Function</em>  callback - fn(err)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">update</span> = <span class="keyword">function</span> <span class="variable">update</span> (<span class="variable">doc</span>, <span class="variable">callback</span>) {
  <span class="this">this</span>.<span class="variable">op</span> = <span class="string">'update'</span>;
  <span class="this">this</span>.<span class="variable">_updateArg</span> = <span class="variable">doc</span>;

  <span class="keyword">var</span> <span class="variable">model</span> = <span class="this">this</span>.<span class="variable">model</span>
    , <span class="variable">options</span> = <span class="this">this</span>.<span class="variable">_optionsForExec</span>(<span class="variable">model</span>)
    , <span class="variable">fn</span> = <span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">callback</span>
    , <span class="variable">castQuery</span>
    , <span class="variable">castDoc</span>

  <span class="keyword">try</span> {
    <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">model</span>);
    <span class="variable">castQuery</span> = <span class="this">this</span>.<span class="variable">_conditions</span>;
  } <span class="keyword">catch</span> (<span class="variable">err</span>) {
    <span class="keyword">if</span> (<span class="variable">fn</span>) <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>);
    <span class="keyword">throw</span> <span class="variable">err</span>;
  }

  <span class="keyword">try</span> {
    <span class="variable">castDoc</span> = <span class="this">this</span>.<span class="variable">_castUpdate</span>(<span class="variable">doc</span>);
  } <span class="keyword">catch</span> (<span class="variable">err</span>) {
    <span class="keyword">if</span> (<span class="variable">fn</span>) <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>);
    <span class="keyword">throw</span> <span class="variable">err</span>;
  }

  <span class="keyword">if</span> (!<span class="variable">fn</span>) {
    <span class="keyword">delete</span> <span class="variable">options</span>.<span class="variable">safe</span>;
  }

  <span class="keyword">if</span> (<span class="variable">castDoc</span>) {
    <span class="variable">model</span>.<span class="variable">collection</span>.<span class="variable">update</span>(<span class="variable">castQuery</span>, <span class="variable">castDoc</span>, <span class="variable">options</span>, <span class="variable">tick</span>(<span class="variable">callback</span>));
  } <span class="keyword">else</span> {
    <span class="variable">process</span>.<span class="variable">nextTick</span>(<span class="keyword">function</span> () {
      <span class="variable">callback</span>(<span class="keyword">null</span>, <span class="number integer">0</span>);
    });
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>remove</p>

<p>Casts the query, sends the remove command to
mongodb where the query contents, and then
invokes a callback upon receiving the command
result.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
  <span class="this">this</span>.<span class="variable">op</span> = <span class="string">'remove'</span>;

  <span class="keyword">var</span> <span class="variable">model</span> = <span class="this">this</span>.<span class="variable">model</span>
    , <span class="variable">options</span> = <span class="this">this</span>.<span class="variable">_optionsForExec</span>(<span class="variable">model</span>)
    , <span class="variable">cb</span> = <span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">callback</span>

  <span class="keyword">try</span> {
    <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">model</span>);
  } <span class="keyword">catch</span> (<span class="variable">err</span>) {
    <span class="keyword">if</span> (<span class="variable">cb</span>) <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>);
    <span class="keyword">throw</span> <span class="variable">err</span>;
  }

  <span class="keyword">if</span> (!<span class="variable">cb</span>) {
    <span class="keyword">delete</span> <span class="variable">options</span>.<span class="variable">safe</span>;
  }

  <span class="keyword">var</span> <span class="variable">castQuery</span> = <span class="this">this</span>.<span class="variable">_conditions</span>;
  <span class="variable">model</span>.<span class="variable">collection</span>.<span class="variable">remove</span>(<span class="variable">castQuery</span>, <span class="variable">options</span>, <span class="variable">tick</span>(<span class="variable">callback</span>));
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>populate</p>

<p>Sets population options.
 - <strong>api</strong>: <em>public</em></p>

<p> </p>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">populate</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">fields</span>, <span class="variable">conditions</span>, <span class="variable">options</span>) {
  <span class="comment">// The order of fields/conditions args is opposite Model.find but</span>
  <span class="comment">// necessary to keep backward compatibility (fields could be</span>
  <span class="comment">// an array, string, or object literal).</span>
  <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">populate</span>[<span class="variable">path</span>] =
    <span class="keyword">new</span> <span class="class">PopulateOptions</span>(<span class="variable">fields</span>, <span class="variable">conditions</span>, <span class="variable">options</span>);

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>stream</p>

<p>Returns a stream interface</p>

<h2>Example</h2>

<pre><code>Thing.find({ name: /^hello/ }).stream().pipe(res)</code></pre>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">stream</span> = <span class="keyword">function</span> <span class="variable">stream</span> () {
  <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">QueryStream</span>(<span class="this">this</span>);
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Deprecated methods.
 </p>
</td>
<td class="code">
<pre><code><span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">or</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$or'</span>, <span class="string">'Query#or'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">or</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">nor</span> =<span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$nor'</span>, <span class="string">'Query#nor'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">nor</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">run</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#run'</span>, <span class="string">'Query#exec'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">exec</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">mod</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$mod'</span>, <span class="string">'Query#mod'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">mod</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">box</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$box'</span>, <span class="string">'Query#box'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">box</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">near</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$near'</span>, <span class="string">'Query#near'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">near</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">slice</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$slice'</span>, <span class="string">'Query#slice'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">slice</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">notEqualTo</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#notEqualTo'</span>, <span class="string">'Query#ne'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">ne</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">fields</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#fields'</span>, <span class="string">'Query#select'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">select</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">exists</span> =<span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$exists'</span>, <span class="string">'Query#exists'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">exists</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">center</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$center'</span>, <span class="string">'Query#center'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">center</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">elemMatch</span> =<span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$elemMatch'</span>, <span class="string">'Query#elemMatch'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">elemMatch</span>);
<span class="class">Query</span>.<span class="variable">prototype</span>.$<span class="variable">centerSphere</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#$centerSphere'</span>, <span class="string">'Query#centerSphere'</span>, <span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">centerSphere</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>asc</p>

<p>Sorts ascending.</p>

<pre><code>query.asc('name', 'age');</code></pre>

<ul><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">asc</span> () {
  <span class="keyword">var</span> <span class="variable">sort</span> = <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">sort</span> || (<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">sort</span> = []);
  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="variable">arguments</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
    <span class="variable">sort</span>.<span class="variable">push</span>([<span class="variable">arguments</span>[<span class="variable">i</span>], <span class="number integer">1</span>]);
  }
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">asc</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#asc'</span>, <span class="string">'Query#sort'</span>, <span class="variable">asc</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>desc</p>

<p>Sorts descending.</p>

<pre><code>query.desc('name', 'age');</code></pre>

<ul><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">desc</span> () {
  <span class="keyword">var</span> <span class="variable">sort</span> = <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">sort</span> || (<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">sort</span> = []);
  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="variable">arguments</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
    <span class="variable">sort</span>.<span class="variable">push</span>([<span class="variable">arguments</span>[<span class="variable">i</span>], -<span class="number integer">1</span>]);
  }
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">desc</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#desc'</span>, <span class="string">'Query#sort'</span>, <span class="variable">desc</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>limit, skip, maxscan, snapshot, batchSize, comment</p>

<p>Sets these associated options.</p>

<pre><code>query.comment('feed query');</code></pre>

<ul><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="string">'$within wherein $wherein'</span>.<span class="variable">split</span>(<span class="string">' '</span>).<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">getter</span>) {
  <span class="keyword">var</span> <span class="variable">withinDep</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#'</span> + <span class="variable">getter</span>, <span class="string">'Query#within'</span>)
  <span class="class">Object</span>.<span class="variable">defineProperty</span>(<span class="class">Query</span>.<span class="variable">prototype</span>, <span class="variable">getter</span>, {
    <span class="variable">get</span>: <span class="keyword">function</span> () {
      <span class="variable">withinDep</span>();
      <span class="keyword">return</span> <span class="this">this</span>;
    }
  });
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>only</p>

<p>Chainable method for adding the specified fields to the
object of fields to only include.</p>

<h2>Examples</h2>

<pre><code>query.only('a b c');
query.only('a', 'b', 'c');
query.only(['a', 'b', 'c']);</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String | Array</em>  space separated list of fields OR</p><p>                  an array of field names
We can also take arguments as the "array" of field names</p></li><li><p><strong>api</strong>: <em>public</em></p></li><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">only</span> (<span class="variable">fields</span>) {
  <span class="variable">fields</span> = <span class="this">this</span>.<span class="variable">_parseOnlyExcludeFields</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
  <span class="this">this</span>.<span class="variable">_applyFields</span>({ <span class="variable">only</span>: <span class="variable">fields</span> });
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">only</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#only'</span>, <span class="string">'Query#select'</span>, <span class="variable">only</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>exclude</p>

<p>Chainable method for adding the specified fields to the
object of fields to exclude.</p>

<h2>Examples</h2>

<pre><code>query.exclude('a b c');
query.exclude('a', 'b', 'c');
query.exclude(['a', 'b', 'c']);</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String | Array</em>  space separated list of fields OR</p><p>                  an array of field names
We can also take arguments as the "array" of field names</p></li><li><p><strong>api</strong>: <em>public</em></p></li><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">exclude</span> (<span class="variable">fields</span>) {
  <span class="variable">fields</span> = <span class="this">this</span>.<span class="variable">_parseOnlyExcludeFields</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
  <span class="this">this</span>.<span class="variable">_applyFields</span>({ <span class="variable">exclude</span>: <span class="variable">fields</span> });
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">exclude</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#exclude'</span>, <span class="string">'Query#select'</span>, <span class="variable">exclude</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>each()</p>

<p>Streaming cursors.</p>

<p>The <code>callback</code> is called repeatedly for each document
found in the collection as it's streamed. If an error
occurs streaming stops.</p>

<h2>Example</h2>

<pre><code>query.each(function (err, user) {
  if (err) return res.end("aww, received an error. all done.");
  if (user) {
    res.write(user.name + '\n')
  } else {
    res.end("reached end of cursor. all done.");
  }
});</code></pre>

<p>A third parameter may also be used in the callback which
allows you to iterate the cursor manually.</p>

<h2>Example</h2>

<pre><code>query.each(function (err, user, next) {
  if (err) return res.end("aww, received an error. all done.");
  if (user) {
    res.write(user.name + '\n')
    doSomethingAsync(next);
  } else {
    res.end("reached end of cursor. all done.");
  }
});</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>return</strong>: <em>Query</em> </p></li><li><p><strong>deprecate</strong>: <em>d</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">each</span> (<span class="variable">callback</span>) {
  <span class="keyword">var</span> <span class="variable">model</span> = <span class="this">this</span>.<span class="variable">model</span>
    , <span class="variable">options</span> = <span class="this">this</span>.<span class="variable">_optionsForExec</span>(<span class="variable">model</span>)
    , <span class="variable">manual</span> = <span class="number integer">3</span> == <span class="variable">callback</span>.<span class="variable">length</span>
    , <span class="variable">self</span> = <span class="this">this</span>

  <span class="keyword">try</span> {
    <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">model</span>);
  } <span class="keyword">catch</span> (<span class="variable">err</span>) {
    <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>);
  }

  <span class="keyword">var</span> <span class="variable">fields</span> = <span class="variable">utils</span>.<span class="variable">clone</span>(<span class="variable">options</span>.<span class="variable">fields</span> = <span class="this">this</span>.<span class="variable">_fields</span>);

  <span class="keyword">function</span> <span class="variable">complete</span> (<span class="variable">err</span>, <span class="variable">val</span>) {
    <span class="keyword">if</span> (<span class="variable">complete</span>.<span class="variable">ran</span>) <span class="keyword">return</span>;
    <span class="variable">complete</span>.<span class="variable">ran</span> = <span class="variable">true</span>;
    <span class="variable">callback</span>(<span class="variable">err</span>, <span class="variable">val</span>);
  }

  <span class="variable">model</span>.<span class="variable">collection</span>.<span class="variable">find</span>(<span class="this">this</span>.<span class="variable">_conditions</span>, <span class="variable">options</span>, <span class="keyword">function</span> (<span class="variable">err</span>, <span class="variable">cursor</span>) {
    <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">complete</span>(<span class="variable">err</span>);

    <span class="keyword">var</span> <span class="variable">ticks</span> = <span class="number integer">0</span>;
    <span class="variable">next</span>();

    <span class="keyword">function</span> <span class="variable">next</span> () {
      <span class="comment">// nextTick is necessary to avoid stack overflows when</span>
      <span class="comment">// dealing with large result sets. yield occasionally.</span>
      <span class="keyword">if</span> (!(++<span class="variable">ticks</span> % <span class="number integer">20</span>)) {
        <span class="variable">process</span>.<span class="variable">nextTick</span>(<span class="keyword">function</span> () {
          <span class="variable">cursor</span>.<span class="variable">nextObject</span>(<span class="variable">onNextObject</span>);
        });
      } <span class="keyword">else</span> {
        <span class="variable">cursor</span>.<span class="variable">nextObject</span>(<span class="variable">onNextObject</span>);
      }
    }

    <span class="keyword">function</span> <span class="variable">onNextObject</span> (<span class="variable">err</span>, <span class="variable">doc</span>) {
      <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">complete</span>(<span class="variable">err</span>);

      <span class="comment">// when doc is null we hit the end of the cursor</span>
      <span class="keyword">if</span> (!<span class="variable">doc</span>) <span class="keyword">return</span> <span class="variable">complete</span>(<span class="keyword">null</span>, <span class="keyword">null</span>);

      <span class="keyword">var</span> <span class="variable">instance</span> = <span class="keyword">new</span> <span class="variable">model</span>(<span class="variable">undefined</span>, <span class="variable">fields</span>);

      <span class="comment">// skip _id for pre-init hooks</span>
      <span class="keyword">delete</span> <span class="variable">instance</span>.<span class="variable">_doc</span>.<span class="variable">_id</span>;

      <span class="variable">instance</span>.<span class="variable">init</span>(<span class="variable">doc</span>, <span class="variable">self</span>, <span class="keyword">function</span> (<span class="variable">err</span>) {
        <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">complete</span>(<span class="variable">err</span>);

        <span class="keyword">if</span> (<span class="variable">manual</span>) {
          <span class="variable">callback</span>(<span class="keyword">null</span>, <span class="variable">instance</span>, <span class="variable">next</span>);
        } <span class="keyword">else</span> {
          <span class="variable">callback</span>(<span class="keyword">null</span>, <span class="variable">instance</span>);
          <span class="variable">next</span>();
        }
      });
    }

  });

  <span class="keyword">return</span> <span class="this">this</span>;
}
<span class="class">Query</span>.<span class="variable">prototype</span>.<span class="variable">each</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'Query#each'</span>, <span class="string">'Query#stream'</span>, <span class="variable">each</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">Query</span>;
<span class="variable">module</span>.<span class="variable">exports</span>.<span class="class">QueryStream</span> = <span class="class">QueryStream</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/array.js"><a href="#">array</a></h2></td><td>lib/schema/array.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>)
  , <span class="class">CastError</span> = <span class="class">SchemaType</span>.<span class="class">CastError</span>
  , <span class="class">NumberSchema</span> = <span class="variable">require</span>(<span class="string">'./number'</span>)
  , <span class="class">Types</span> = {
        <span class="class">Boolean</span>: <span class="variable">require</span>(<span class="string">'./boolean'</span>)
      , <span class="class">Date</span>: <span class="variable">require</span>(<span class="string">'./date'</span>)
      , <span class="class">Number</span>: <span class="class">ArrayNumberSchema</span>
      , <span class="class">String</span>: <span class="variable">require</span>(<span class="string">'./string'</span>)
      , <span class="class">ObjectId</span>: <span class="variable">require</span>(<span class="string">'./objectid'</span>)
      , <span class="class">Buffer</span>: <span class="variable">require</span>(<span class="string">'./buffer'</span>)
    }
  , <span class="class">MongooseArray</span> = <span class="variable">require</span>(<span class="string">'../types'</span>).<span class="class">Array</span>
  , <span class="class">Mixed</span> = <span class="variable">require</span>(<span class="string">'./mixed'</span>)
  , <span class="class">Query</span> = <span class="variable">require</span>(<span class="string">'../query'</span>)
  , <span class="variable">isMongooseObject</span> = <span class="variable">require</span>(<span class="string">'../utils'</span>).<span class="variable">isMongooseObject</span></code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">SchemaArray</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">SchemaArray</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/boolean.js"><a href="#">boolean</a></h2></td><td>lib/schema/boolean.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">SchemaBoolean</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">SchemaBoolean</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/buffer.js"><a href="#">buffer</a></h2></td><td>lib/schema/buffer.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>)
  , <span class="class">CastError</span> = <span class="class">SchemaType</span>.<span class="class">CastError</span>
  , <span class="class">BufferNumberSchema</span> = <span class="keyword">function</span> () {}
  , <span class="class">MongooseBuffer</span> = <span class="variable">require</span>(<span class="string">'../types'</span>).<span class="class">Buffer</span>
  , <span class="class">Binary</span> = <span class="class">MongooseBuffer</span>.<span class="class">Binary</span>
  , <span class="class">Query</span> = <span class="variable">require</span>(<span class="string">'../query'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">SchemaBuffer</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">SchemaBuffer</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/date.js"><a href="#">date</a></h2></td><td>lib/schema/date.js</td></tr><tr class="code">
<td class="docs">
<p>Module requirements.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>)
  , <span class="class">CastError</span> = <span class="class">SchemaType</span>.<span class="class">CastError</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">SchemaDate</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">SchemaDate</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/documentarray.js"><a href="#">documentarray</a></h2></td><td>lib/schema/documentarray.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>)
  , <span class="class">ArrayType</span> = <span class="variable">require</span>(<span class="string">'./array'</span>)
  , <span class="class">MongooseDocumentArray</span> = <span class="variable">require</span>(<span class="string">'../types/documentarray'</span>)
  , <span class="class">Subdocument</span> = <span class="variable">require</span>(<span class="string">'../types/embedded'</span>)
  , <span class="class">CastError</span> = <span class="class">SchemaType</span>.<span class="class">CastError</span>
  , <span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'../document'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from ArrayType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">DocumentArray</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">ArrayType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">DocumentArray</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/index.js"><a href="#">index</a></h2></td><td>lib/schema/index.js</td></tr><tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">String</span> = <span class="variable">require</span>(<span class="string">'./string'</span>);

<span class="variable">exports</span>.<span class="class">Number</span> = <span class="variable">require</span>(<span class="string">'./number'</span>);

<span class="variable">exports</span>.<span class="class">Boolean</span> = <span class="variable">require</span>(<span class="string">'./boolean'</span>);

<span class="variable">exports</span>.<span class="class">DocumentArray</span> = <span class="variable">require</span>(<span class="string">'./documentarray'</span>);

<span class="variable">exports</span>.<span class="class">Array</span> = <span class="variable">require</span>(<span class="string">'./array'</span>);

<span class="variable">exports</span>.<span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'./buffer'</span>);

<span class="variable">exports</span>.<span class="class">Date</span> = <span class="variable">require</span>(<span class="string">'./date'</span>);

<span class="variable">exports</span>.<span class="class">ObjectId</span> = <span class="variable">require</span>(<span class="string">'./objectid'</span>);

<span class="variable">exports</span>.<span class="class">Mixed</span> = <span class="variable">require</span>(<span class="string">'./mixed'</span>);
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/mixed.js"><a href="#">mixed</a></h2></td><td>lib/schema/mixed.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">Mixed</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">Mixed</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/number.js"><a href="#">number</a></h2></td><td>lib/schema/number.js</td></tr><tr class="code">
<td class="docs">
<p>Module requirements.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>)
  , <span class="class">CastError</span> = <span class="class">SchemaType</span>.<span class="class">CastError</span>
  , <span class="class">MongooseNumber</span> = <span class="variable">require</span>(<span class="string">'../types/number'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">SchemaNumber</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets a maximum number validator</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Number</em>  minimum number</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaNumber</span>.<span class="variable">prototype</span>.<span class="variable">min</span> = <span class="keyword">function</span> (<span class="variable">value</span>, <span class="variable">message</span>) {
  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">minValidator</span>)
    <span class="this">this</span>.<span class="variable">validators</span> = <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">filter</span>(<span class="keyword">function</span>(<span class="variable">v</span>){
      <span class="keyword">return</span> <span class="variable">v</span>[<span class="number integer">1</span>] != <span class="string">'min'</span>;
    });
  <span class="keyword">if</span> (<span class="variable">value</span> != <span class="keyword">null</span>)
    <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">push</span>([<span class="keyword">function</span>(<span class="variable">v</span>){
      <span class="keyword">return</span> <span class="variable">v</span> === <span class="keyword">null</span> || <span class="variable">v</span> &<span class="variable">gt</span>;= <span class="variable">value</span>;
    }, <span class="string">'min'</span>]);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets a maximum number validator</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Number</em>  maximum number</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaNumber</span>.<span class="variable">prototype</span>.<span class="variable">max</span> = <span class="keyword">function</span> (<span class="variable">value</span>, <span class="variable">message</span>) {
  <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">maxValidator</span>)
    <span class="this">this</span>.<span class="variable">validators</span> = <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">filter</span>(<span class="keyword">function</span>(<span class="variable">v</span>){
      <span class="keyword">return</span> <span class="variable">v</span>[<span class="number integer">1</span>] != <span class="string">'max'</span>;
    });
  <span class="keyword">if</span> (<span class="variable">value</span> != <span class="keyword">null</span>)
    <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">push</span>([<span class="this">this</span>.<span class="variable">maxValidator</span> = <span class="keyword">function</span>(<span class="variable">v</span>){
      <span class="keyword">return</span> <span class="variable">v</span> === <span class="keyword">null</span> || <span class="variable">v</span> &<span class="variable">lt</span>;= <span class="variable">value</span>;
    }, <span class="string">'max'</span>]);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">SchemaNumber</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/objectid.js"><a href="#">objectid</a></h2></td><td>lib/schema/objectid.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>)
  , <span class="class">CastError</span> = <span class="class">SchemaType</span>.<span class="class">CastError</span>
  , <span class="variable">driver</span> = <span class="variable">global</span>.<span class="class">MONGOOSE_DRIVER_PATH</span> || <span class="string">'./../drivers/node-mongodb-native'</span>
  , <span class="variable">oid</span> = <span class="variable">require</span>(<span class="string">'../types/objectid'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">ObjectId</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">ObjectId</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema/string.js"><a href="#">string</a></h2></td><td>lib/schema/string.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SchemaType</span> = <span class="variable">require</span>(<span class="string">'../schematype'</span>)
  , <span class="class">CastError</span> = <span class="class">SchemaType</span>.<span class="class">CastError</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from SchemaType.
 </p>
</td>
<td class="code">
<pre><code><span class="class">SchemaString</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">SchemaType</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds enumeration values</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>multiple</em>  enumeration values</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaString</span>.<span class="variable">prototype</span>.<span class="keyword">enum</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">len</span> = <span class="variable">arguments</span>.<span class="variable">length</span>;
  <span class="keyword">if</span> (!<span class="variable">len</span> || <span class="variable">undefined</span> === <span class="variable">arguments</span>[<span class="number integer">0</span>] || <span class="variable">false</span> === <span class="variable">arguments</span>[<span class="number integer">0</span>]) {
    <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">enumValidator</span>){
      <span class="this">this</span>.<span class="variable">enumValidator</span> = <span class="variable">false</span>;
      <span class="this">this</span>.<span class="variable">validators</span> = <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">filter</span>(<span class="keyword">function</span>(<span class="variable">v</span>){
        <span class="keyword">return</span> <span class="variable">v</span>[<span class="number integer">1</span>] != <span class="string">'enum'</span>;
      });
    }
    <span class="keyword">return</span>;
  }

  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; <span class="variable">i</span>++) {
    <span class="keyword">if</span> (<span class="variable">undefined</span> !== <span class="variable">arguments</span>[<span class="variable">i</span>]) {
      <span class="this">this</span>.<span class="variable">enumValues</span>.<span class="variable">push</span>(<span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">arguments</span>[<span class="variable">i</span>]));
    }
  }

  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">enumValidator</span>) {
    <span class="keyword">var</span> <span class="variable">values</span> = <span class="this">this</span>.<span class="variable">enumValues</span>;
    <span class="this">this</span>.<span class="variable">enumValidator</span> = <span class="keyword">function</span>(<span class="variable">v</span>){
      <span class="keyword">return</span> ~<span class="variable">values</span>.<span class="variable">indexOf</span>(<span class="variable">v</span>);
    };
    <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">push</span>([<span class="this">this</span>.<span class="variable">enumValidator</span>, <span class="string">'enum'</span>]);
  }
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a lowercase setter</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaString</span>.<span class="variable">prototype</span>.<span class="variable">lowercase</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">set</span>(<span class="keyword">function</span> (<span class="variable">v</span>) {
    <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">toLowerCase</span>();
  });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds an uppercase setter</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaString</span>.<span class="variable">prototype</span>.<span class="variable">uppercase</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">set</span>(<span class="keyword">function</span> (<span class="variable">v</span>) {
    <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">toUpperCase</span>();
  });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a trim setter</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaString</span>.<span class="variable">prototype</span>.<span class="variable">trim</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">set</span>(<span class="keyword">function</span> (<span class="variable">v</span>) {
    <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">trim</span>();
  });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets a regexp test</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>RegExp</em>  regular expression to test against</p></li><li><p><strong>param</strong>: <em>String</em>  optional validator message</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaString</span>.<span class="variable">prototype</span>.<span class="variable">match</span> = <span class="keyword">function</span>(<span class="variable">regExp</span>){
  <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">push</span>([<span class="keyword">function</span>(<span class="variable">v</span>){
    <span class="keyword">return</span> <span class="variable">regExp</span>.<span class="variable">test</span>(<span class="variable">v</span>);
  }, <span class="string">'regexp'</span>]);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">SchemaString</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schema.js"><a href="#">schema</a></h2></td><td>lib/schema.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">EventEmitter</span> = <span class="variable">require</span>(<span class="string">'events'</span>).<span class="class">EventEmitter</span>
  , <span class="class">VirtualType</span> = <span class="variable">require</span>(<span class="string">'./virtualtype'</span>)
  , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>)
  , <span class="class">NamedScope</span>
  , <span class="class">Query</span>
  , <span class="class">Types</span></code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Schema constructor.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  definition</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">Schema</span> (<span class="variable">obj</span>, <span class="variable">options</span>) {
  <span class="this">this</span>.<span class="variable">paths</span> = {};
  <span class="this">this</span>.<span class="variable">virtuals</span> = {};
  <span class="this">this</span>.<span class="variable">nested</span> = {};
  <span class="this">this</span>.<span class="variable">inherits</span> = {};
  <span class="this">this</span>.<span class="variable">callQueue</span> = [];
  <span class="this">this</span>.<span class="variable">_indexes</span> = [];
  <span class="this">this</span>.<span class="variable">methods</span> = {};
  <span class="this">this</span>.<span class="variable">statics</span> = {};
  <span class="this">this</span>.<span class="variable">tree</span> = {};

  <span class="comment">// set options</span>
  <span class="this">this</span>.<span class="variable">options</span> = <span class="variable">utils</span>.<span class="variable">options</span>({
      <span class="variable">safe</span>: <span class="variable">true</span>
    , <span class="variable">strict</span>: <span class="variable">false</span>
  }, <span class="variable">options</span>);

  <span class="comment">// build paths</span>
  <span class="keyword">if</span> (<span class="variable">obj</span>)
    <span class="this">this</span>.<span class="variable">add</span>(<span class="variable">obj</span>);

  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">paths</span>[<span class="string">'_id'</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">noId</span>) {
    <span class="this">this</span>.<span class="variable">add</span>({ <span class="variable">_id</span>: {<span class="variable">type</span>: <span class="class">ObjectId</span>, <span class="variable">auto</span>: <span class="variable">true</span>} });
  }

  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">paths</span>[<span class="string">'id'</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="this">this</span>.<span class="variable">options</span>.<span class="variable">noVirtualId</span>) {
    <span class="this">this</span>.<span class="variable">virtual</span>(<span class="string">'id'</span>).<span class="variable">get</span>(<span class="keyword">function</span> () {
      <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">__id</span>) {
        <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">__id</span>;
      }

      <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">__id</span> = <span class="keyword">null</span> == <span class="this">this</span>.<span class="variable">_id</span>
        ? <span class="keyword">null</span>
        : <span class="this">this</span>.<span class="variable">_id</span>.<span class="variable">toString</span>();
    });
  }

  <span class="keyword">delete</span> <span class="this">this</span>.<span class="variable">options</span>.<span class="variable">noVirtualId</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from EventEmitter.
 </p>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">EventEmitter</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets the keys</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  keys</p></li><li><p><strong>param</strong>: <em>String</em>  prefix</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">add</span> = <span class="keyword">function</span> <span class="variable">add</span> (<span class="variable">obj</span>, <span class="variable">prefix</span>) {
  <span class="variable">prefix</span> = <span class="variable">prefix</span> || <span class="string">''</span>;
  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">obj</span>) {
    <span class="keyword">if</span> (<span class="keyword">null</span> == <span class="variable">obj</span>[<span class="variable">i</span>]) {
      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">TypeError</span>(<span class="string">'Invalid value for schema path `'</span>+ <span class="variable">prefix</span> + <span class="variable">i</span> +<span class="string">'`'</span>);
    }

    <span class="keyword">if</span> (<span class="variable">obj</span>[<span class="variable">i</span>].<span class="variable">constructor</span>.<span class="variable">name</span> == <span class="string">'Object'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; (!<span class="variable">obj</span>[<span class="variable">i</span>].<span class="variable">type</span> || <span class="variable">obj</span>[<span class="variable">i</span>].<span class="variable">type</span>.<span class="variable">type</span>)) {
      <span class="keyword">if</span> (<span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">obj</span>[<span class="variable">i</span>]).<span class="variable">length</span>) {
        <span class="comment">// nested object { last: { name: String }}</span>
        <span class="this">this</span>.<span class="variable">nested</span>[<span class="variable">prefix</span> + <span class="variable">i</span>] = <span class="variable">true</span>;
        <span class="this">this</span>.<span class="variable">add</span>(<span class="variable">obj</span>[<span class="variable">i</span>], <span class="variable">prefix</span> + <span class="variable">i</span> + <span class="string">'.'</span>);
      }
      <span class="keyword">else</span>
        <span class="this">this</span>.<span class="variable">path</span>(<span class="variable">prefix</span> + <span class="variable">i</span>, <span class="variable">obj</span>[<span class="variable">i</span>]); <span class="comment">// mixed type</span>
    } <span class="keyword">else</span>
      <span class="this">this</span>.<span class="variable">path</span>(<span class="variable">prefix</span> + <span class="variable">i</span>, <span class="variable">obj</span>[<span class="variable">i</span>]);
  }
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets a path (if arity 2)
Gets a path (if arity 1)</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>param</strong>: <em>Object</em>  constructor</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">path</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">obj</span>) {
  <span class="keyword">if</span> (<span class="variable">obj</span> == <span class="variable">undefined</span>) {
    <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">paths</span>[<span class="variable">path</span>]) <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">paths</span>[<span class="variable">path</span>];

    <span class="comment">// Sometimes path will come in as</span>
    <span class="comment">// pathNameA.4.pathNameB where 4 means the index</span>
    <span class="comment">// of an embedded document in an embedded array.</span>
    <span class="comment">// In this case, we need to jump to the Array's</span>
    <span class="comment">// schema and call path() from there to resolve to</span>
    <span class="comment">// the correct path type</span>

    <span class="keyword">var</span> <span class="variable">last</span>
      , <span class="variable">self</span> = <span class="this">this</span>
      , <span class="variable">subpaths</span> = <span class="variable">path</span>.<span class="variable">split</span>(<span class="regexp">/\.(\d+)\.?/</span>)
                       .<span class="variable">filter</span>(<span class="class">Boolean</span>) <span class="comment">// removes empty strings</span>

    <span class="keyword">if</span> (<span class="variable">subpaths</span>.<span class="variable">length</span> &<span class="variable">gt</span>; <span class="number integer">1</span>) {
      <span class="variable">last</span> = <span class="variable">subpaths</span>.<span class="variable">length</span> - <span class="number integer">1</span>;
      <span class="keyword">return</span> <span class="variable">subpaths</span>.<span class="variable">reduce</span>(<span class="keyword">function</span> (<span class="variable">val</span>, <span class="variable">subpath</span>, <span class="variable">i</span>) {
        <span class="keyword">if</span> (<span class="variable">val</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="variable">val</span>.<span class="variable">schema</span>) {
          <span class="keyword">if</span> (<span class="variable">i</span> === <span class="variable">last</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="regexp">/\D/</span>.<span class="variable">test</span>(<span class="variable">subpath</span>) &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">val</span> <span class="variable">instanceof</span> <span class="class">Types</span>.<span class="class">Array</span>) {
            <span class="keyword">return</span> <span class="variable">val</span>.<span class="variable">caster</span>; <span class="comment">// StringSchema, NumberSchema, etc</span>
          } <span class="keyword">else</span> {
            <span class="keyword">return</span> <span class="variable">val</span>;
          }
        }

        <span class="keyword">if</span> (!<span class="regexp">/\D/</span>.<span class="variable">test</span>(<span class="variable">subpath</span>)) { <span class="comment">// 'path.0.subpath'  on path 0</span>
          <span class="keyword">return</span> <span class="variable">val</span>;
        }

        <span class="keyword">return</span> <span class="variable">val</span> ? <span class="variable">val</span>.<span class="variable">schema</span>.<span class="variable">path</span>(<span class="variable">subpath</span>)
                   : <span class="variable">self</span>.<span class="variable">path</span>(<span class="variable">subpath</span>);
      }, <span class="keyword">null</span>);
    }

    <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">paths</span>[<span class="variable">subpaths</span>[<span class="number integer">0</span>]];
  }

  <span class="comment">// update the tree</span>
  <span class="keyword">var</span> <span class="variable">subpaths</span> = <span class="variable">path</span>.<span class="variable">split</span>(<span class="regexp">/\./</span>)
    , <span class="variable">last</span> = <span class="variable">subpaths</span>.<span class="variable">pop</span>()
    , <span class="variable">branch</span> = <span class="this">this</span>.<span class="variable">tree</span>;

  <span class="variable">subpaths</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">path</span>) {
    <span class="keyword">if</span> (!<span class="variable">branch</span>[<span class="variable">path</span>]) <span class="variable">branch</span>[<span class="variable">path</span>] = {};
    <span class="variable">branch</span> = <span class="variable">branch</span>[<span class="variable">path</span>];
  });

  <span class="variable">branch</span>[<span class="variable">last</span>] = <span class="variable">utils</span>.<span class="variable">clone</span>(<span class="variable">obj</span>);

  <span class="this">this</span>.<span class="variable">paths</span>[<span class="variable">path</span>] = <span class="class">Schema</span>.<span class="variable">interpretAsType</span>(<span class="variable">path</span>, <span class="variable">obj</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Converts -- e.g., Number, [SomeSchema], 
{ type: String, enum: ['m', 'f'] } -- into
the appropriate Mongoose Type, which we use
later for casting, validation, etc.
## </p>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>param</strong>: <em>Object</em>  constructor</p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">interpretAsType</span> = <span class="keyword">function</span> (<span class="variable">path</span>, <span class="variable">obj</span>) {
  <span class="keyword">if</span> (<span class="variable">obj</span>.<span class="variable">constructor</span>.<span class="variable">name</span> != <span class="string">'Object'</span>)
    <span class="variable">obj</span> = { <span class="variable">type</span>: <span class="variable">obj</span> };

  <span class="comment">// Get the type making sure to allow keys named &quot;type&quot;</span>
  <span class="comment">// and default to mixed if not specified.</span>
  <span class="comment">// { type: { type: String, default: 'freshcut' } }</span>
  <span class="keyword">var</span> <span class="variable">type</span> = <span class="variable">obj</span>.<span class="variable">type</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="variable">obj</span>.<span class="variable">type</span>.<span class="variable">type</span>
    ? <span class="variable">obj</span>.<span class="variable">type</span>
    : {};

  <span class="keyword">if</span> (<span class="variable">type</span>.<span class="variable">constructor</span>.<span class="variable">name</span> == <span class="string">'Object'</span>) {
    <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Types</span>.<span class="class">Mixed</span>(<span class="variable">path</span>, <span class="variable">obj</span>);
  }

  <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">type</span>) || <span class="variable">type</span> == <span class="class">Array</span>) {
    <span class="comment">// if it was specified through { type } look for `cast`</span>
    <span class="keyword">var</span> <span class="variable">cast</span> = <span class="variable">type</span> == <span class="class">Array</span>
      ? <span class="variable">obj</span>.<span class="variable">cast</span>
      : <span class="variable">type</span>[<span class="number integer">0</span>];

    <span class="keyword">if</span> (<span class="variable">cast</span> <span class="variable">instanceof</span> <span class="class">Schema</span>) {
      <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Types</span>.<span class="class">DocumentArray</span>(<span class="variable">path</span>, <span class="variable">cast</span>, <span class="variable">obj</span>);
    }

    <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Types</span>.<span class="class">Array</span>(<span class="variable">path</span>, <span class="variable">cast</span> || <span class="class">Types</span>.<span class="class">Mixed</span>, <span class="variable">obj</span>);
  }

  <span class="keyword">if</span> (<span class="variable">undefined</span> == <span class="class">Types</span>[<span class="variable">type</span>.<span class="variable">name</span>]) {
    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">TypeError</span>(<span class="string">'Undefined type at `'</span> + <span class="variable">path</span> +
        <span class="string">'`\n  Did you try nesting Schemas? '</span> +
        <span class="string">'You can only nest using refs or arrays.'</span>);
  }

  <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Types</span>[<span class="variable">type</span>.<span class="variable">name</span>](<span class="variable">path</span>, <span class="variable">obj</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Iterates through the schema's paths, passing the path string and type object
to the callback.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  callback function - fn(pathstring, type)</p></li><li><p><strong>return</strong>: <em>Schema</em>  this for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">eachPath</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">var</span> <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">paths</span>)
    , <span class="variable">len</span> = <span class="variable">keys</span>.<span class="variable">length</span>;

  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {
    <span class="variable">fn</span>(<span class="variable">keys</span>[<span class="variable">i</span>], <span class="this">this</span>.<span class="variable">paths</span>[<span class="variable">keys</span>[<span class="variable">i</span>]]);
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns an Array of path strings that are required.
 - <strong>api</strong>: <em>public</em></p>

<p> </p>
</td>
<td class="code">
<pre><code><span class="class">Object</span>.<span class="variable">defineProperty</span>(<span class="class">Schema</span>.<span class="variable">prototype</span>, <span class="string">'requiredPaths'</span>, {
  <span class="variable">get</span>: <span class="keyword">function</span> () {
    <span class="keyword">var</span> <span class="variable">paths</span> = <span class="this">this</span>.<span class="variable">paths</span>
      , <span class="variable">pathnames</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">paths</span>)
      , <span class="variable">i</span> = <span class="variable">pathnames</span>.<span class="variable">length</span>
      , <span class="variable">pathname</span>, <span class="variable">path</span>
      , <span class="variable">requiredPaths</span> = [];
    <span class="keyword">while</span> (<span class="variable">i</span>--) {
      <span class="variable">pathname</span> = <span class="variable">pathnames</span>[<span class="variable">i</span>];
      <span class="variable">path</span> = <span class="variable">paths</span>[<span class="variable">pathname</span>];
      <span class="keyword">if</span> (<span class="variable">path</span>.<span class="variable">isRequired</span>) <span class="variable">requiredPaths</span>.<span class="variable">push</span>(<span class="variable">pathname</span>);
    }
    <span class="keyword">return</span> <span class="variable">requiredPaths</span>;
  }
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Given a path, returns whether it is a real, virtual,
nested, or ad-hoc/undefined path.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">pathType</span> = <span class="keyword">function</span> (<span class="variable">path</span>) {
  <span class="keyword">if</span> (<span class="variable">path</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">paths</span>) <span class="keyword">return</span> <span class="string">'real'</span>;
  <span class="keyword">if</span> (<span class="variable">path</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">virtuals</span>) <span class="keyword">return</span> <span class="string">'virtual'</span>;
  <span class="keyword">if</span> (<span class="variable">path</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">nested</span>) <span class="keyword">return</span> <span class="string">'nested'</span>;
  <span class="keyword">return</span> <span class="string">'adhocOrUndefined'</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Defines a pre for the document</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  method</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">pre</span> = <span class="keyword">function</span>(){
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">queue</span>(<span class="string">'pre'</span>, <span class="variable">arguments</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Defines a post hook for the document.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  method</p></li><li><p><strong>param</strong>: <em>Function</em>  callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">post</span> = <span class="keyword">function</span>(<span class="variable">method</span>, <span class="variable">fn</span>){
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">queue</span>(<span class="string">'on'</span>, <span class="variable">arguments</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Registers a plugin for this schema</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  plugin callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">plugin</span> = <span class="keyword">function</span> (<span class="variable">fn</span>, <span class="variable">opts</span>) {
  <span class="variable">fn</span>(<span class="this">this</span>, <span class="variable">opts</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a method</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  method name</p></li><li><p><strong>param</strong>: <em>Function</em>  handler</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">method</span> = <span class="keyword">function</span> (<span class="variable">name</span>, <span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="string">'string'</span> != <span class="keyword">typeof</span> <span class="variable">name</span>)
    <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">name</span>)
      <span class="this">this</span>.<span class="variable">methods</span>[<span class="variable">i</span>] = <span class="variable">name</span>[<span class="variable">i</span>];
  <span class="keyword">else</span>
    <span class="this">this</span>.<span class="variable">methods</span>[<span class="variable">name</span>] = <span class="variable">fn</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Defines a static method</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  name</p></li><li><p><strong>param</strong>: <em>Function</em>  handler</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">static</span> = <span class="keyword">function</span>(<span class="variable">name</span>, <span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="string">'string'</span> != <span class="keyword">typeof</span> <span class="variable">name</span>)
    <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">name</span>)
      <span class="this">this</span>.<span class="variable">statics</span>[<span class="variable">i</span>] = <span class="variable">name</span>[<span class="variable">i</span>];
  <span class="keyword">else</span>
    <span class="this">this</span>.<span class="variable">statics</span>[<span class="variable">name</span>] = <span class="variable">fn</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Defines an index (most likely compound)
## Example
   schema.index({ first: 1, last: -1 })</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  field</p></li><li><p><strong>param</strong>: <em>Object</em>  optional options object</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">index</span> = <span class="keyword">function</span> (<span class="variable">fields</span>, <span class="variable">options</span>) {
  <span class="this">this</span>.<span class="variable">_indexes</span>.<span class="variable">push</span>([<span class="variable">fields</span>, <span class="variable">options</span> || {}]);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets/gets an option</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  key</p></li><li><p><strong>param</strong>: <em>Object</em>  optional value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">key</span>, <span class="variable">value</span>) {
  <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> == <span class="number integer">1</span>)
    <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">options</span>[<span class="variable">key</span>];
  <span class="this">this</span>.<span class="variable">options</span>[<span class="variable">key</span>] = <span class="variable">value</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Compiles indexes from fields and schema-level indexes</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'indexes'</span>, <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">indexes</span> = []
    , <span class="variable">seenSchemas</span> = [];

  <span class="variable">collectIndexes</span>(<span class="this">this</span>);

  <span class="keyword">return</span> <span class="variable">indexes</span>;

  <span class="keyword">function</span> <span class="variable">collectIndexes</span> (<span class="variable">schema</span>, <span class="variable">prefix</span>) {
    <span class="keyword">if</span> (~<span class="variable">seenSchemas</span>.<span class="variable">indexOf</span>(<span class="variable">schema</span>)) <span class="keyword">return</span>;
    <span class="variable">seenSchemas</span>.<span class="variable">push</span>(<span class="variable">schema</span>);

    <span class="keyword">var</span> <span class="variable">index</span>;
    <span class="keyword">var</span> <span class="variable">paths</span> = <span class="variable">schema</span>.<span class="variable">paths</span>;
    <span class="variable">prefix</span> = <span class="variable">prefix</span> || <span class="string">''</span>;

    <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">paths</span>) {
      <span class="keyword">if</span> (<span class="variable">paths</span>[<span class="variable">i</span>]) {
        <span class="keyword">if</span> (<span class="variable">paths</span>[<span class="variable">i</span>] <span class="variable">instanceof</span> <span class="class">Types</span>.<span class="class">DocumentArray</span>) {
          <span class="variable">collectIndexes</span>(<span class="variable">paths</span>[<span class="variable">i</span>].<span class="variable">schema</span>, <span class="variable">i</span> + <span class="string">'.'</span>);
        } <span class="keyword">else</span> {
          <span class="variable">index</span> = <span class="variable">paths</span>[<span class="variable">i</span>].<span class="variable">_index</span>;

          <span class="keyword">if</span> (<span class="variable">index</span> !== <span class="variable">false</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">index</span> !== <span class="keyword">null</span>){
            <span class="keyword">var</span> <span class="variable">field</span> = {};
            <span class="variable">field</span>[<span class="variable">prefix</span> + <span class="variable">i</span>] = <span class="string">'2d'</span> === <span class="variable">index</span> ? <span class="variable">index</span> : <span class="number integer">1</span>;
            <span class="variable">indexes</span>.<span class="variable">push</span>([<span class="variable">field</span>, <span class="string">'Object'</span> === <span class="variable">index</span>.<span class="variable">constructor</span>.<span class="variable">name</span> ? <span class="variable">index</span> : {} ]);
          }
        }
      }
    }

    <span class="keyword">if</span> (<span class="variable">prefix</span>) {
      <span class="variable">fixSubIndexPaths</span>(<span class="variable">schema</span>, <span class="variable">prefix</span>);
    } <span class="keyword">else</span> {
      <span class="variable">indexes</span> = <span class="variable">indexes</span>.<span class="variable">concat</span>(<span class="variable">schema</span>.<span class="variable">_indexes</span>);
    }
  }</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Checks for indexes added to subdocs using Schema.index().
These indexes need their paths prefixed properly.</p>

<p>schema._indexes = [ [indexObj, options], [indexObj, options] ..]
   </p>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">fixSubIndexPaths</span> (<span class="variable">schema</span>, <span class="variable">prefix</span>) {
    <span class="keyword">var</span> <span class="variable">subindexes</span> = <span class="variable">schema</span>.<span class="variable">_indexes</span>
      , <span class="variable">len</span> = <span class="variable">subindexes</span>.<span class="variable">length</span>
      , <span class="variable">indexObj</span>
      , <span class="variable">newindex</span>
      , <span class="variable">klen</span>
      , <span class="variable">keys</span>
      , <span class="variable">key</span>
      , <span class="variable">i</span> = <span class="number integer">0</span>
      , <span class="variable">j</span>

    <span class="keyword">for</span> (<span class="variable">i</span> = <span class="number integer">0</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {
      <span class="variable">indexObj</span> = <span class="variable">subindexes</span>[<span class="variable">i</span>][<span class="number integer">0</span>];
      <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">indexObj</span>);
      <span class="variable">klen</span> = <span class="variable">keys</span>.<span class="variable">length</span>;
      <span class="variable">newindex</span> = {};

      <span class="comment">// use forward iteration, order matters</span>
      <span class="keyword">for</span> (<span class="variable">j</span> = <span class="number integer">0</span>; <span class="variable">j</span> &<span class="variable">lt</span>; <span class="variable">klen</span>; ++<span class="variable">j</span>) {
        <span class="variable">key</span> = <span class="variable">keys</span>[<span class="variable">j</span>];
        <span class="variable">newindex</span>[<span class="variable">prefix</span> + <span class="variable">key</span>] = <span class="variable">indexObj</span>[<span class="variable">key</span>];
      }

      <span class="variable">indexes</span>.<span class="variable">push</span>([<span class="variable">newindex</span>, <span class="variable">subindexes</span>[<span class="variable">i</span>][<span class="number integer">1</span>]]);
    }
  }

});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Retrieves or creates the virtual type with the given name.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  name</p></li><li><p><strong>return</strong>: <em>VirtualType</em> </p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">virtual</span> = <span class="keyword">function</span> (<span class="variable">name</span>, <span class="variable">options</span>) {
  <span class="keyword">var</span> <span class="variable">virtuals</span> = <span class="this">this</span>.<span class="variable">virtuals</span> || (<span class="this">this</span>.<span class="variable">virtuals</span> = {});
  <span class="keyword">var</span> <span class="variable">parts</span> = <span class="variable">name</span>.<span class="variable">split</span>(<span class="string">'.'</span>);
  <span class="keyword">return</span> <span class="variable">virtuals</span>[<span class="variable">name</span>] = <span class="variable">parts</span>.<span class="variable">reduce</span>(<span class="keyword">function</span> (<span class="variable">mem</span>, <span class="variable">part</span>, <span class="variable">i</span>) {
    <span class="variable">mem</span>[<span class="variable">part</span>] || (<span class="variable">mem</span>[<span class="variable">part</span>] = (<span class="variable">i</span> === <span class="variable">parts</span>.<span class="variable">length</span>-<span class="number integer">1</span>)
                            ? <span class="keyword">new</span> <span class="class">VirtualType</span>(<span class="variable">options</span>)
                            : {});
    <span class="keyword">return</span> <span class="variable">mem</span>[<span class="variable">part</span>];
  }, <span class="this">this</span>.<span class="variable">tree</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Fetches the virtual type with the given name.
Should be distinct from virtual because virtual auto-defines a new VirtualType
if the path doesn't exist.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  name</p></li><li><p><strong>return</strong>: <em>VirtualType</em> </p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">virtualpath</span> = <span class="keyword">function</span> (<span class="variable">name</span>) {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">virtuals</span>[<span class="variable">name</span>];
};

<span class="class">Schema</span>.<span class="variable">prototype</span>.<span class="variable">namedScope</span> = <span class="keyword">function</span> (<span class="variable">name</span>, <span class="variable">fn</span>) {
  <span class="keyword">var</span> <span class="variable">namedScopes</span> = <span class="this">this</span>.<span class="variable">namedScopes</span> || (<span class="this">this</span>.<span class="variable">namedScopes</span> = <span class="keyword">new</span> <span class="class">NamedScope</span>)
    , <span class="variable">newScope</span> = <span class="class">Object</span>.<span class="variable">create</span>(<span class="variable">namedScopes</span>)
    , <span class="variable">allScopes</span> = <span class="variable">namedScopes</span>.<span class="variable">scopesByName</span> || (<span class="variable">namedScopes</span>.<span class="variable">scopesByName</span> = {});
  <span class="variable">allScopes</span>[<span class="variable">name</span>] = <span class="variable">newScope</span>;
  <span class="variable">newScope</span>.<span class="variable">name</span> = <span class="variable">name</span>;
  <span class="variable">newScope</span>.<span class="variable">block</span> = <span class="variable">fn</span>;
  <span class="variable">newScope</span>.<span class="variable">query</span> = <span class="keyword">new</span> <span class="class">Query</span>();
  <span class="variable">newScope</span>.<span class="variable">decorate</span>(<span class="variable">namedScopes</span>, {
    <span class="variable">block0</span>: <span class="keyword">function</span> (<span class="variable">block</span>) {
      <span class="keyword">return</span> <span class="keyword">function</span> () {
        <span class="variable">block</span>.<span class="variable">call</span>(<span class="this">this</span>.<span class="variable">query</span>);
        <span class="keyword">return</span> <span class="this">this</span>;
      };
    },
    <span class="variable">blockN</span>: <span class="keyword">function</span> (<span class="variable">block</span>) {
      <span class="keyword">return</span> <span class="keyword">function</span> () {
        <span class="variable">block</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">query</span>, <span class="variable">arguments</span>);
        <span class="keyword">return</span> <span class="this">this</span>;
      };
    },
    <span class="variable">basic</span>: <span class="keyword">function</span> (<span class="variable">query</span>) {
      <span class="keyword">return</span> <span class="keyword">function</span> () {
        <span class="this">this</span>.<span class="variable">query</span>.<span class="variable">find</span>(<span class="variable">query</span>);
        <span class="keyword">return</span> <span class="this">this</span>;
      };
    }
  });
  <span class="keyword">return</span> <span class="variable">newScope</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>ObjectId schema identifier. Not an actual ObjectId, only used for Schemas.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">ObjectId</span> () {
  <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'This is an abstract interface. Its only purpose is to mark '</span>
                + <span class="string">'fields as ObjectId in the schema creation.'</span>);
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">exports</span> = <span class="class">Schema</span>;

<span class="comment">// require down here because of reference issues</span>
<span class="variable">exports</span>.<span class="class">Types</span> = <span class="class">Types</span> = <span class="variable">require</span>(<span class="string">'./schema/index'</span>);
<span class="class">NamedScope</span> = <span class="variable">require</span>(<span class="string">'./namedscope'</span>)
<span class="class">Query</span> = <span class="variable">require</span>(<span class="string">'./query'</span>);

<span class="variable">exports</span>.<span class="class">ObjectId</span> = <span class="class">ObjectId</span>;

</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schemadefault.js"><a href="#">schemadefault</a></h2></td><td>lib/schemadefault.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Schema</span> = <span class="variable">require</span>(<span class="string">'./schema'</span>)</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Default model for querying the system.profiles
collection (it only exists when profiling is
enabled.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>[<span class="string">'system.profile'</span>] = <span class="keyword">new</span> <span class="class">Schema</span>({
    <span class="variable">ts</span>: <span class="class">Date</span>
  , <span class="variable">info</span>: <span class="class">String</span> <span class="comment">// deprecated</span>
  , <span class="variable">millis</span>: <span class="class">Number</span>
  , <span class="variable">op</span>: <span class="class">String</span>
  , <span class="variable">ns</span>: <span class="class">String</span>
  , <span class="variable">query</span>: <span class="class">Schema</span>.<span class="class">Types</span>.<span class="class">Mixed</span>
  , <span class="variable">updateobj</span>: <span class="class">Schema</span>.<span class="class">Types</span>.<span class="class">Mixed</span>
  , <span class="variable">ntoreturn</span>: <span class="class">Number</span>
  , <span class="variable">nreturned</span>: <span class="class">Number</span>
  , <span class="variable">nscanned</span>: <span class="class">Number</span>
  , <span class="variable">responseLength</span>: <span class="class">Number</span>
  , <span class="variable">client</span>: <span class="class">String</span>
  , <span class="variable">user</span>: <span class="class">String</span>
  , <span class="variable">idhack</span>: <span class="class">Boolean</span>
  , <span class="variable">scanAndOrder</span>: <span class="class">Boolean</span>
  , <span class="variable">keyUpdates</span>: <span class="class">Number</span>
  , <span class="variable">cursorid</span>: <span class="class">Number</span>
}, { <span class="variable">noVirtualId</span>: <span class="variable">true</span>, <span class="variable">noId</span>: <span class="variable">true</span> });
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/schematype.js"><a href="#">schematype</a></h2></td><td>lib/schematype.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">MongooseError</span> = <span class="variable">require</span>(<span class="string">'./error'</span>);
<span class="keyword">var</span> <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>SchemaType constructor</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>String</em>  path</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">SchemaType</span> (<span class="variable">path</span>, <span class="variable">options</span>, <span class="variable">instance</span>) {
  <span class="this">this</span>.<span class="variable">path</span> = <span class="variable">path</span>;
  <span class="this">this</span>.<span class="variable">instance</span> = <span class="variable">instance</span>;
  <span class="this">this</span>.<span class="variable">validators</span> = [];
  <span class="this">this</span>.<span class="variable">setters</span> = [];
  <span class="this">this</span>.<span class="variable">getters</span> = [];
  <span class="this">this</span>.<span class="variable">options</span> = <span class="variable">options</span>;
  <span class="this">this</span>.<span class="variable">_index</span> = <span class="keyword">null</span>;
  <span class="this">this</span>.<span class="variable">selected</span>;

  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">options</span>) <span class="keyword">if</span> (<span class="this">this</span>[<span class="variable">i</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="this">this</span>[<span class="variable">i</span>]) {
    <span class="keyword">var</span> <span class="variable">opts</span> = <span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>[<span class="variable">i</span>])
      ? <span class="variable">options</span>[<span class="variable">i</span>]
      : [<span class="variable">options</span>[<span class="variable">i</span>]];

    <span class="this">this</span>[<span class="variable">i</span>].<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">opts</span>);
  }
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets a default</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  default value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaType</span>.<span class="variable">prototype</span>.<span class="keyword">default</span> = <span class="keyword">function</span> (<span class="variable">val</span>) {
  <span class="keyword">if</span> (<span class="number integer">1</span> === <span class="variable">arguments</span>.<span class="variable">length</span>) {
    <span class="this">this</span>.<span class="variable">defaultValue</span> = <span class="keyword">typeof</span> <span class="variable">val</span> === <span class="string">'function'</span>
      ? <span class="variable">val</span>
      : <span class="this">this</span>.<span class="variable">cast</span>(<span class="variable">val</span>);
    <span class="keyword">return</span> <span class="this">this</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> &<span class="variable">gt</span>; <span class="number integer">1</span>) {
    <span class="this">this</span>.<span class="variable">defaultValue</span> = <span class="variable">utils</span>.<span class="variable">args</span>(<span class="variable">arguments</span>);
  }
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">defaultValue</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets index. It can be a boolean or a hash of options
## Example
   Schema.path('my.path').index(true);
   Schema.path('my.path').index({ unique: true });</p>

<p>"Direction doesn't matter for single key indexes"
http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeysIndexes</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  true/</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaType</span>.<span class="variable">prototype</span>.<span class="variable">index</span> = <span class="keyword">function</span> (<span class="variable">index</span>) {
  <span class="this">this</span>.<span class="variable">_index</span> = <span class="variable">index</span>;
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a setter</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  setter</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaType</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="string">'function'</span> != <span class="keyword">typeof</span> <span class="variable">fn</span>)
    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'A setter must be a function.'</span>);
  <span class="this">this</span>.<span class="variable">setters</span>.<span class="variable">push</span>(<span class="variable">fn</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a getter</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  getter</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaType</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="string">'function'</span> != <span class="keyword">typeof</span> <span class="variable">fn</span>)
    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'A getter must be a function.'</span>);
  <span class="this">this</span>.<span class="variable">getters</span>.<span class="variable">push</span>(<span class="variable">fn</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<h2>validate</h2>

<p>Adds validators.</p>

<h2>Examples</h2>

<pre><code>function validator () { ... }

var single = [validator, 'failed']
new Schema({ name: { type: String, validate: single }});

var many = [
    { validator: validator, msg: 'uh oh' }
  , { validator: fn, msg: 'failed' }
]
new Schema({ name: { type: String, validate: many }});</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  validator</p></li><li><p><strong>param</strong>: <em>String</em>  optional error message</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaType</span>.<span class="variable">prototype</span>.<span class="variable">validate</span> = <span class="keyword">function</span> (<span class="variable">obj</span>, <span class="variable">error</span>) {
  <span class="keyword">if</span> (<span class="string">'function'</span> == <span class="keyword">typeof</span> <span class="variable">obj</span> || <span class="variable">obj</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'RegExp'</span> === <span class="variable">obj</span>.<span class="variable">constructor</span>.<span class="variable">name</span>) {
    <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">push</span>([<span class="variable">obj</span>, <span class="variable">error</span>]);
    <span class="keyword">return</span> <span class="this">this</span>;
  }

  <span class="keyword">var</span> <span class="variable">i</span> = <span class="variable">arguments</span>.<span class="variable">length</span>
    , <span class="variable">arg</span>

  <span class="keyword">while</span> (<span class="variable">i</span>--) {
    <span class="variable">arg</span> = <span class="variable">arguments</span>[<span class="variable">i</span>];
    <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">push</span>([<span class="variable">arg</span>.<span class="variable">validator</span>, <span class="variable">arg</span>.<span class="variable">msg</span>]);
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a required validator</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Boolean</em>  enable/disable the validator</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SchemaType</span>.<span class="variable">prototype</span>.<span class="variable">required</span> = <span class="keyword">function</span> (<span class="variable">required</span>) {
  <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;

  <span class="keyword">function</span> <span class="variable">__checkRequired</span> (<span class="variable">v</span>) {
    <span class="comment">// in here, `this` refers to the validating document.</span>
    <span class="comment">// no validation when this path wasn't selected in the query.</span>
    <span class="keyword">if</span> (<span class="string">'isSelected'</span> <span class="keyword">in</span> <span class="this">this</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>;
        !<span class="this">this</span>.<span class="variable">isSelected</span>(<span class="variable">self</span>.<span class="variable">path</span>) &<span class="variable">amp</span>;&<span class="variable">amp</span>;
        !<span class="this">this</span>.<span class="variable">isModified</span>(<span class="variable">self</span>.<span class="variable">path</span>)) <span class="keyword">return</span> <span class="variable">true</span>;
    <span class="keyword">return</span> <span class="variable">self</span>.<span class="variable">checkRequired</span>(<span class="variable">v</span>);
  }

  <span class="keyword">if</span> (<span class="variable">false</span> === <span class="variable">required</span>) {
    <span class="this">this</span>.<span class="variable">isRequired</span> = <span class="variable">false</span>;
    <span class="this">this</span>.<span class="variable">validators</span> = <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">filter</span>(<span class="keyword">function</span> (<span class="variable">v</span>) {
      <span class="keyword">return</span> <span class="variable">v</span>[<span class="number integer">0</span>].<span class="variable">name</span> !== <span class="string">'__checkRequired'</span>;
    });
  } <span class="keyword">else</span> {
    <span class="this">this</span>.<span class="variable">isRequired</span> = <span class="variable">true</span>;
    <span class="this">this</span>.<span class="variable">validators</span>.<span class="variable">push</span>([<span class="variable">__checkRequired</span>, <span class="string">'required'</span>]);
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<h2>select</h2>

<p>Set default select() behavior for this path. True if
this path should always be included in the results,
false if it should be excluded by default. This setting
can be overridden at the query level.</p>

<pre><code>T = db.model('T', new Schema({ x: { type: String, select: true }}));
T.find(..); // x will always be selected ..
// .. unless overridden;
T.find().select({ x: 0 }).exec();</code></pre>

<p> </p>
</td>
<td class="code">
<pre><code><span class="class">SchemaType</span>.<span class="variable">prototype</span>.<span class="variable">select</span> = <span class="keyword">function</span> <span class="variable">select</span> (<span class="variable">val</span>) {
  <span class="this">this</span>.<span class="variable">selected</span> = !! <span class="variable">val</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from MongooseError
 </p>
</td>
<td class="code">
<pre><code><span class="class">ValidatorError</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">MongooseError</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from MongooseError.
 </p>
</td>
<td class="code">
<pre><code><span class="class">CastError</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">MongooseError</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">exports</span> = <span class="class">SchemaType</span>;

<span class="variable">exports</span>.<span class="class">CastError</span> = <span class="class">CastError</span>;

<span class="variable">exports</span>.<span class="class">ValidatorError</span> = <span class="class">ValidatorError</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/statemachine.js"><a href="#">statemachine</a></h2></td><td>lib/statemachine.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>);</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/array.js"><a href="#">array</a></h2></td><td>lib/types/array.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">EmbeddedDocument</span> = <span class="variable">require</span>(<span class="string">'./embedded'</span>);
<span class="keyword">var</span> <span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'../document'</span>);
<span class="keyword">var</span> <span class="class">ObjectId</span> = <span class="variable">require</span>(<span class="string">'./objectid'</span>);
<span class="keyword">var</span> <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'../utils'</span>)</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from Array
 </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span> = <span class="keyword">new</span> <span class="class">Array</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Marks this array as modified.
It is called during a nonAtomicPush, an atomic opteration,
or by an existing embedded document that is modified.</p>

<p>If it bubbles up from an embedded document change,
then it takes the following arguments (otherwise, takes
0 arguments)
## </p>

<ul><li><p><strong>param</strong>: <em>EmbeddedDocument</em>  embeddedDoc that invokes this method on the Array</p></li><li><p><strong>param</strong>: <em>String</em>  embeddedPath is what changed inthe embeddedDoc</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">_markModified</span> = <span class="keyword">function</span> (<span class="variable">embeddedDoc</span>, <span class="variable">embeddedPath</span>) {
  <span class="keyword">var</span> <span class="variable">parent</span> = <span class="this">this</span>.<span class="variable">_parent</span>
    , <span class="variable">dirtyPath</span>;

  <span class="keyword">if</span> (<span class="variable">parent</span>) {
    <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span>) {
      <span class="comment">// If an embedded doc bubbled up the change</span>
      <span class="variable">dirtyPath</span> = [<span class="this">this</span>.<span class="variable">_path</span>, <span class="this">this</span>.<span class="variable">indexOf</span>(<span class="variable">embeddedDoc</span>), <span class="variable">embeddedPath</span>].<span class="variable">join</span>(<span class="string">'.'</span>);
    } <span class="keyword">else</span> {
      <span class="variable">dirtyPath</span> = <span class="this">this</span>.<span class="variable">_path</span>;
    }
    <span class="variable">parent</span>.<span class="variable">markModified</span>(<span class="variable">dirtyPath</span>);
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns true if we have to perform atomics for this, and no normal
operations</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'doAtomics'</span>, <span class="keyword">function</span> () {
  <span class="keyword">if</span> (!(<span class="this">this</span>.<span class="variable">_atomics</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'Object'</span> === <span class="this">this</span>.<span class="variable">_atomics</span>.<span class="variable">constructor</span>.<span class="variable">name</span>)) {
    <span class="keyword">return</span> <span class="number integer">0</span>;
  }

  <span class="keyword">return</span> <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">_atomics</span>).<span class="variable">length</span>;
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pushes item/s to the array atomically. Overrides Array#push</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">push</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">values</span> = [].<span class="variable">map</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="this">this</span>.<span class="variable">_cast</span>, <span class="this">this</span>)
    , <span class="variable">ret</span> = [].<span class="variable">push</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">values</span>);

  <span class="comment">// $pushAll might be fibbed (could be $push). But it makes it easier to</span>
  <span class="comment">// handle what could have been $push, $pushAll combos</span>
  <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pushAll'</span>, <span class="variable">values</span>);
  <span class="keyword">return</span> <span class="variable">ret</span>;
};
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">push</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseArray#$push'</span>
          , <span class="string">'MongooseArray#push'</span>, <span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">push</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pushes item/s to the array non-atomically</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">nonAtomicPush</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">values</span> = [].<span class="variable">map</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="this">this</span>.<span class="variable">_cast</span>, <span class="this">this</span>)
    , <span class="variable">ret</span> = [].<span class="variable">push</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">values</span>);
  <span class="this">this</span>.<span class="variable">_markModified</span>();
  <span class="keyword">return</span> <span class="variable">ret</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pushes several items at once to the array atomically</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Array</em>  values</p></li><li><p><strong>api</strong>: <em>public</em></p></li><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> $<span class="variable">pushAll</span> (<span class="variable">value</span>) {
  <span class="keyword">var</span> <span class="variable">length</span> = <span class="this">this</span>.<span class="variable">length</span>;
  <span class="this">this</span>.<span class="variable">nonAtomicPush</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">value</span>);
  <span class="comment">// make sure we access the casted elements</span>
  <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pushAll'</span>, <span class="this">this</span>.<span class="variable">slice</span>(<span class="variable">length</span>));
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">pushAll</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseArray#$pushAll'</span>
          , <span class="string">'MongooseArray#push in v3'</span>, $<span class="variable">pushAll</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pops the array atomically</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">pop</span> =
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">pop</span> = <span class="keyword">function</span> () {
  <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pop'</span>, <span class="number integer">1</span>);
  <span class="keyword">return</span> [].<span class="variable">pop</span>.<span class="variable">call</span>(<span class="this">this</span>)
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Atomically shifts the array.</p>

<p>Only works once per doc.save(). Calling this mulitple
times on an array before saving is the same as calling
it once.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">shift</span> =
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">shift</span> = <span class="keyword">function</span> $<span class="variable">shift</span> () {
  <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pop'</span>, -<span class="number integer">1</span>);
  <span class="keyword">return</span> [].<span class="variable">shift</span>.<span class="variable">call</span>(<span class="this">this</span>)
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Removes items from an array atomically</p>

<h2>Examples</h2>

<pre><code>doc.array.remove(ObjectId)
doc.array.remove('tag 1', 'tag 2')</code></pre>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  value to remove</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">args</span> = [].<span class="variable">map</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="this">this</span>.<span class="variable">_cast</span>, <span class="this">this</span>);
  <span class="keyword">if</span> (<span class="variable">args</span>.<span class="variable">length</span> == <span class="number integer">1</span>)
    <span class="this">this</span>.$<span class="variable">pull</span>(<span class="variable">args</span>[<span class="number integer">0</span>]);
  <span class="keyword">else</span>
    <span class="this">this</span>.$<span class="variable">pullAll</span>(<span class="variable">args</span>);
  <span class="keyword">return</span> <span class="variable">args</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pulls from the array</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">pull</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">values</span> = [].<span class="variable">map</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="this">this</span>.<span class="variable">_cast</span>, <span class="this">this</span>)
    , <span class="variable">cur</span> = <span class="this">this</span>.<span class="variable">_parent</span>.<span class="variable">get</span>(<span class="this">this</span>.<span class="variable">_path</span>)
    , <span class="variable">i</span> = <span class="variable">cur</span>.<span class="variable">length</span>
    , <span class="variable">mem</span>;

  <span class="keyword">while</span> (<span class="variable">i</span>--) {
    <span class="variable">mem</span> = <span class="variable">cur</span>[<span class="variable">i</span>];
    <span class="keyword">if</span> (<span class="variable">mem</span> <span class="variable">instanceof</span> <span class="class">EmbeddedDocument</span>) {
      <span class="keyword">if</span> (<span class="variable">values</span>.<span class="variable">some</span>(<span class="keyword">function</span> (<span class="variable">v</span>) { <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">equals</span>(<span class="variable">mem</span>); } )) {
        [].<span class="variable">splice</span>.<span class="variable">call</span>(<span class="variable">cur</span>, <span class="variable">i</span>, <span class="number integer">1</span>);
      }
    } <span class="keyword">else</span> <span class="keyword">if</span> (~<span class="variable">cur</span>.<span class="variable">indexOf</span>.<span class="variable">call</span>(<span class="variable">values</span>, <span class="variable">mem</span>)) {
      [].<span class="variable">splice</span>.<span class="variable">call</span>(<span class="variable">cur</span>, <span class="variable">i</span>, <span class="number integer">1</span>);
    }
  }

  <span class="keyword">if</span> (<span class="variable">values</span>[<span class="number integer">0</span>] <span class="variable">instanceof</span> <span class="class">EmbeddedDocument</span>) {
    <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pullDocs'</span>, <span class="variable">values</span>.<span class="variable">map</span>( <span class="keyword">function</span> (<span class="variable">v</span>) { <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">_id</span>; } ));
  } <span class="keyword">else</span> {
    <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pullAll'</span>, <span class="variable">values</span>);
  }

  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">pull</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseArray#$pull'</span>
          , <span class="string">'MongooseArray#pull'</span>, <span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">pull</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pulls many items from an array</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li><li><p><strong>deprecate</strong>: <em>d</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> $<span class="variable">pullAll</span> (<span class="variable">values</span>) {
  <span class="keyword">if</span> (<span class="variable">values</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">values</span>.<span class="variable">length</span>) {
    <span class="keyword">var</span> <span class="variable">oldArr</span> = <span class="this">this</span>.<span class="variable">_parent</span>.<span class="variable">get</span>(<span class="this">this</span>.<span class="variable">_path</span>)
      , <span class="variable">i</span> = <span class="variable">oldArr</span>.<span class="variable">length</span>, <span class="variable">mem</span>;
    <span class="keyword">while</span> (<span class="variable">i</span>--) {
      <span class="variable">mem</span> = <span class="variable">oldArr</span>[<span class="variable">i</span>];
      <span class="keyword">if</span> (<span class="variable">mem</span> <span class="variable">instanceof</span> <span class="class">EmbeddedDocument</span>) {
        <span class="keyword">if</span> (<span class="variable">values</span>.<span class="variable">some</span>( <span class="keyword">function</span> (<span class="variable">v</span>) { <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">equals</span>(<span class="variable">mem</span>); } )) {
          <span class="variable">oldArr</span>.<span class="variable">splice</span>(<span class="variable">i</span>, <span class="number integer">1</span>);
        }
      } <span class="keyword">else</span> <span class="keyword">if</span> (~<span class="variable">values</span>.<span class="variable">indexOf</span>(<span class="variable">mem</span>)) <span class="variable">oldArr</span>.<span class="variable">splice</span>(<span class="variable">i</span>, <span class="number integer">1</span>);
    }
    <span class="keyword">if</span> (<span class="variable">values</span>[<span class="number integer">0</span>] <span class="variable">instanceof</span> <span class="class">EmbeddedDocument</span>) {
      <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pullDocs'</span>, <span class="variable">values</span>.<span class="variable">map</span>( <span class="keyword">function</span> (<span class="variable">v</span>) { <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">_id</span>; } ));
    } <span class="keyword">else</span> {
      <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$pullAll'</span>, <span class="variable">values</span>);
    }
  }
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">pullAll</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseArray#$pullAll'</span>, <span class="string">'MongooseArray#pull'</span>, $<span class="variable">pullAll</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Splices the array.</p>

<h2>Note marks the _entire_ array as modified which</h2>

<p>will pass the entire thing to $set potentially
overwritting any changes that happen between
when you retrieved the object and when you save
it.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">splice</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">result</span> = [].<span class="variable">splice</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
  <span class="this">this</span>.<span class="variable">_markModified</span>();
  <span class="keyword">return</span> <span class="variable">result</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Non-atomically unshifts onto the array.</p>

<h2>Note marks the _entire_ array as modified which</h2>

<p>will pass the entire thing to $set potentially
overwritting any changes that happen between
when you retrieved the object and when you save
it.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">unshift</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">values</span> = [].<span class="variable">map</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="this">this</span>.<span class="variable">_cast</span>, <span class="this">this</span>);
  [].<span class="variable">unshift</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">values</span>);
  <span class="this">this</span>.<span class="variable">_markModified</span>();
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">length</span>;
};
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">unshift</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseArray#$unshift'</span>
          , <span class="string">'MongooseArray#unshift'</span>, <span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">unshift</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds values to the array if not already present.
 - <strong>api</strong>: <em>public</em></p>

<p> </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">addToSet</span> = <span class="keyword">function</span> <span class="variable">addToSet</span> () {
  <span class="keyword">var</span> <span class="variable">values</span> = [].<span class="variable">map</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="this">this</span>.<span class="variable">_cast</span>, <span class="this">this</span>)
    , <span class="variable">added</span> = []
    , <span class="variable">type</span> = <span class="variable">values</span>[<span class="number integer">0</span>] <span class="variable">instanceof</span> <span class="class">EmbeddedDocument</span> ? <span class="string">'doc'</span> :
             <span class="variable">values</span>[<span class="number integer">0</span>] <span class="variable">instanceof</span> <span class="class">Date</span> ? <span class="string">'date'</span> :
             <span class="string">''</span>;

  <span class="variable">values</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">v</span>) {
    <span class="keyword">var</span> <span class="variable">found</span>;
    <span class="keyword">switch</span> (<span class="variable">type</span>) {
      <span class="keyword">case</span> <span class="string">'doc'</span>:
        <span class="variable">found</span> = <span class="this">this</span>.<span class="variable">some</span>(<span class="keyword">function</span>(<span class="variable">doc</span>){ <span class="keyword">return</span> <span class="variable">doc</span>.<span class="variable">equals</span>(<span class="variable">v</span>) });
        <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="string">'date'</span>:
        <span class="keyword">var</span> <span class="variable">val</span> = +<span class="variable">v</span>;
        <span class="variable">found</span> = <span class="this">this</span>.<span class="variable">some</span>(<span class="keyword">function</span>(<span class="variable">d</span>){ <span class="keyword">return</span> +<span class="variable">d</span> === <span class="variable">val</span> });
        <span class="keyword">break</span>;
      <span class="keyword">default</span>:
        <span class="variable">found</span> = ~<span class="this">this</span>.<span class="variable">indexOf</span>(<span class="variable">v</span>);
    }

    <span class="keyword">if</span> (!<span class="variable">found</span>) {
      [].<span class="variable">push</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">v</span>);
      <span class="this">this</span>.<span class="variable">_registerAtomic</span>(<span class="string">'$addToSet'</span>, <span class="variable">v</span>);
      [].<span class="variable">push</span>.<span class="variable">call</span>(<span class="variable">added</span>, <span class="variable">v</span>);
    }
  }, <span class="this">this</span>);

  <span class="keyword">return</span> <span class="variable">added</span>;
};
<span class="class">MongooseArray</span>.<span class="variable">prototype</span>.$<span class="variable">addToSet</span> =
  <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseArray#$addToSet'</span>
          , <span class="string">'MongooseArray#addToSet'</span>, <span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">addToSet</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns an Array</p>

<h2></h2>

<ul><li><p><strong>return</strong>: <em>Array</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">toObject</span> = <span class="keyword">function</span> (<span class="variable">options</span>) {
  <span class="keyword">if</span> (<span class="variable">options</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">options</span>.<span class="variable">depopulate</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>[<span class="number integer">0</span>] <span class="variable">instanceof</span> <span class="class">Document</span>) {
    <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">doc</span>) {
      <span class="keyword">return</span> <span class="variable">doc</span>.<span class="variable">_id</span>;
    });
  }

  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">doc</span>) {
    <span class="keyword">return</span> <span class="variable">doc</span>;
  });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Helper for console.log</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">inspect</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="string">'['</span> + <span class="this">this</span>.<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">doc</span>) {
    <span class="keyword">return</span> <span class="string">' '</span> + <span class="variable">doc</span>;
  }) + <span class="string">' ]'</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Return the index of <code>obj</code> or <code>-1.</code></p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  obj</p></li><li><p><strong>return</strong>: <em>Number</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseArray</span>.<span class="variable">prototype</span>.<span class="variable">indexOf</span> = <span class="keyword">function</span> <span class="variable">indexOf</span> (<span class="variable">obj</span>) {
  <span class="keyword">if</span> (<span class="variable">obj</span> <span class="variable">instanceof</span> <span class="class">ObjectId</span>) <span class="variable">obj</span> = <span class="variable">obj</span>.<span class="variable">toString</span>();
  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">len</span> = <span class="this">this</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {
    <span class="keyword">if</span> (<span class="variable">obj</span> == <span class="this">this</span>[<span class="variable">i</span>])
      <span class="keyword">return</span> <span class="variable">i</span>;
  }
  <span class="keyword">return</span> -<span class="number integer">1</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">exports</span> = <span class="class">MongooseArray</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/buffer.js"><a href="#">buffer</a></h2></td><td>lib/types/buffer.js</td></tr><tr class="code">
<td class="docs">
<p>Access driver.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">driver</span> = <span class="variable">global</span>.<span class="class">MONGOOSE_DRIVER_PATH</span> || <span class="string">'../drivers/node-mongodb-native'</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Binary</span> = <span class="variable">require</span>(<span class="variable">driver</span> + <span class="string">'/binary'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from Buffer.
 </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseBuffer</span>.<span class="variable">prototype</span> = <span class="keyword">new</span> <span class="class">Buffer</span>(<span class="number integer">0</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Marks this buffer as modified.</p>

<ul><li><strong>api</strong>: <em>public</em></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseBuffer</span>.<span class="variable">prototype</span>.<span class="variable">_markModified</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">parent</span> = <span class="this">this</span>.<span class="variable">_parent</span>;

  <span class="keyword">if</span> (<span class="variable">parent</span>) {
    <span class="variable">parent</span>.<span class="variable">markModified</span>(<span class="this">this</span>.<span class="variable">_path</span>);
  }
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Writes the buffer.</p>
</td>
<td class="code">
<pre><code><span class="class">MongooseBuffer</span>.<span class="variable">prototype</span>.<span class="variable">write</span> = <span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">written</span> = <span class="class">Buffer</span>.<span class="variable">prototype</span>.<span class="variable">write</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);

  <span class="keyword">if</span> (<span class="variable">written</span> &<span class="variable">gt</span>; <span class="number integer">0</span>) {
    <span class="this">this</span>.<span class="variable">_markModified</span>();
  }

  <span class="keyword">return</span> <span class="variable">written</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Copy the buffer.</p>

<h2>Note Buffer#copy will not mark target as modified so</h2>

<p>you must copy from a MongooseBuffer for it to work
as expected.</p>

<p>Work around since copy modifies the target, not this.</p>
</td>
<td class="code">
<pre><code><span class="class">MongooseBuffer</span>.<span class="variable">prototype</span>.<span class="variable">copy</span> = <span class="keyword">function</span> (<span class="variable">target</span>) {
  <span class="keyword">var</span> <span class="variable">ret</span> = <span class="class">Buffer</span>.<span class="variable">prototype</span>.<span class="variable">copy</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);

  <span class="keyword">if</span> (<span class="variable">target</span> <span class="variable">instanceof</span> <span class="class">MongooseBuffer</span>) {
    <span class="variable">target</span>.<span class="variable">_markModified</span>();
  }

  <span class="keyword">return</span> <span class="variable">ret</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Compile other Buffer methods marking this buffer as modified.
 </p>
</td>
<td class="code">
<pre><code>;(
<span class="comment">// node &lt; 0.5</span>
<span class="string">'writeUInt8 writeUInt16 writeUInt32 writeInt8 writeInt16 writeInt32 '</span> +
<span class="string">'writeFloat writeDouble fill '</span> +
<span class="string">'utf8Write binaryWrite asciiWrite set '</span> +

<span class="comment">// node &gt;= 0.5</span>
<span class="string">'writeUInt16LE writeUInt16BE writeUInt32LE writeUInt32BE '</span> +
<span class="string">'writeInt16LE writeInt16BE writeInt32LE writeInt32BE '</span> +
<span class="string">'writeFloatLE writeFloatBE writeDoubleLE writeDoubleBE'</span>
).<span class="variable">split</span>(<span class="string">' '</span>).<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">method</span>) {
  <span class="keyword">if</span> (!<span class="class">Buffer</span>.<span class="variable">prototype</span>[<span class="variable">method</span>]) <span class="keyword">return</span>;
  <span class="class">MongooseBuffer</span>.<span class="variable">prototype</span>[<span class="variable">method</span>] = <span class="keyword">new</span> <span class="class">Function</span>(
    <span class="string">'var ret = Buffer.prototype.'</span>+<span class="variable">method</span>+<span class="string">'.apply(this, arguments);'</span> +
    <span class="string">'this._markModified();'</span> +
    <span class="string">'return ret;'</span>
  )
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns a Binary.</p>

<h2></h2>

<ul><li><p><strong>return</strong>: <em>Buffer</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseBuffer</span>.<span class="variable">prototype</span>.<span class="variable">toObject</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Binary</span>(<span class="this">this</span>, <span class="variable">x00</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseBuffer</span>.<span class="class">Binary</span> = <span class="class">Binary</span>;

<span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">MongooseBuffer</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/document.js"><a href="#">document</a></h2></td><td>lib/types/document.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'../document'</span>)
  , <span class="variable">inspect</span> = <span class="variable">require</span>(<span class="string">'util'</span>).<span class="variable">inspect</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from Document</p>

<p> </p>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">Document</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Override set to mark the parent as modified</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">oldSet</span> = <span class="class">Document</span>.<span class="variable">prototype</span>.<span class="variable">set</span>;

<span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">path</span>) {
  <span class="this">this</span>.<span class="variable">markModified</span>(<span class="variable">path</span>);
  <span class="keyword">return</span> <span class="variable">oldSet</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Save the subdocument</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">save</span> = <span class="keyword">function</span>(<span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="variable">fn</span>)
    <span class="variable">fn</span>(<span class="keyword">null</span>);
  <span class="this">this</span>.<span class="variable">emit</span>(<span class="string">'save'</span>, <span class="this">this</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove the subdocument</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">var</span> <span class="variable">_id</span>;
  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">willRemove</span>) {
    <span class="variable">_id</span> = <span class="this">this</span>.<span class="variable">_doc</span>.<span class="variable">_id</span>;
    <span class="keyword">if</span> (!<span class="variable">_id</span>) {
      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'For your own good, Mongoose does not know '</span> + 
                      <span class="string">'how to remove an EmbeddedDocument that has no _id'</span>);
    }
    <span class="this">this</span>.<span class="variable">parentArray</span>.$<span class="variable">pull</span>({ <span class="variable">_id</span>: <span class="variable">_id</span> });
    <span class="this">this</span>.<span class="variable">willRemove</span> = <span class="variable">true</span>;
  }

  <span class="keyword">if</span> (<span class="variable">fn</span>)
    <span class="variable">fn</span>(<span class="keyword">null</span>);

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Helper for console.log</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">inspect</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="variable">inspect</span>(<span class="this">this</span>.<span class="variable">toObject</span>());
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exxports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">EmbeddedDocument</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/documentarray.js"><a href="#">documentarray</a></h2></td><td>lib/types/documentarray.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">MongooseArray</span> = <span class="variable">require</span>(<span class="string">'./array'</span>)
  , <span class="variable">driver</span> = <span class="variable">global</span>.<span class="class">MONGOOSE_DRIVER_PATH</span> || <span class="string">'../drivers/node-mongodb-native'</span>
  , <span class="class">ObjectId</span> = <span class="variable">require</span>(<span class="variable">driver</span> + <span class="string">'/objectid'</span>)
  , <span class="class">ObjectIdSchema</span> = <span class="variable">require</span>(<span class="string">'../schema/objectid'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from MongooseArray
 </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseDocumentArray</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">MongooseArray</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Filters items by id</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  id</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseDocumentArray</span>.<span class="variable">prototype</span>.<span class="variable">id</span> = <span class="keyword">function</span> (<span class="variable">id</span>) {
  <span class="keyword">var</span> <span class="variable">casted</span>
    , <span class="variable">_id</span>;

  <span class="keyword">try</span> {
    <span class="variable">casted</span> = <span class="class">ObjectId</span>.<span class="variable">toString</span>(<span class="class">ObjectIdSchema</span>.<span class="variable">prototype</span>.<span class="variable">cast</span>.<span class="variable">call</span>({}, <span class="variable">id</span>));
  } <span class="keyword">catch</span> (<span class="variable">e</span>) {
    <span class="variable">casted</span> = <span class="keyword">null</span>;
  }

  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="this">this</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
    <span class="variable">_id</span> = <span class="this">this</span>[<span class="variable">i</span>].<span class="variable">get</span>(<span class="string">'_id'</span>);
    <span class="keyword">if</span> (!(<span class="variable">_id</span> <span class="variable">instanceof</span> <span class="class">ObjectId</span>)) {
      <span class="keyword">if</span> (<span class="class">String</span>(<span class="variable">id</span>) == <span class="variable">_id</span>)
        <span class="keyword">return</span> <span class="this">this</span>[<span class="variable">i</span>];
    } <span class="keyword">else</span> {
      <span class="keyword">if</span> (<span class="variable">casted</span> == <span class="variable">_id</span>)
        <span class="keyword">return</span> <span class="this">this</span>[<span class="variable">i</span>];
    }
  }

  <span class="keyword">return</span> <span class="keyword">null</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns an Array and converts any Document
members toObject.</p>

<h2></h2>

<ul><li><p><strong>return</strong>: <em>Array</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseDocumentArray</span>.<span class="variable">prototype</span>.<span class="variable">toObject</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">doc</span>) {
    <span class="keyword">return</span> <span class="variable">doc</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">doc</span>.<span class="variable">toObject</span>() || <span class="keyword">null</span>;
  });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Helper for console.log</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseDocumentArray</span>.<span class="variable">prototype</span>.<span class="variable">inspect</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="string">'['</span> + <span class="this">this</span>.<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">doc</span>) {
    <span class="keyword">return</span> <span class="variable">doc</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">doc</span>.<span class="variable">inspect</span>() || <span class="string">'null'</span>;
  }).<span class="variable">join</span>(<span class="string">'\n'</span>) + <span class="string">']'</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">MongooseDocumentArray</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/embedded.js"><a href="#">embedded</a></h2></td><td>lib/types/embedded.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'../document'</span>)
  , <span class="variable">inspect</span> = <span class="variable">require</span>(<span class="string">'util'</span>).<span class="variable">inspect</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from Document
 </p>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">Document</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Noop. Does not actually save the doc to the db.</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">save</span> = <span class="keyword">function</span>(<span class="variable">fn</span>) {
  <span class="keyword">if</span> (<span class="variable">fn</span>)
    <span class="variable">fn</span>(<span class="keyword">null</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove the subdocument</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="keyword">var</span> <span class="variable">_id</span>;
  <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">willRemove</span>) {
    <span class="variable">_id</span> = <span class="this">this</span>.<span class="variable">_doc</span>.<span class="variable">_id</span>;
    <span class="keyword">if</span> (!<span class="variable">_id</span>) {
      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'For your own good, Mongoose does not know '</span> + 
                      <span class="string">'how to remove an EmbeddedDocument that has no _id'</span>);
    }
    <span class="this">this</span>.<span class="variable">parentArray</span>.$<span class="variable">pull</span>({ <span class="variable">_id</span>: <span class="variable">_id</span> });
    <span class="this">this</span>.<span class="variable">willRemove</span> = <span class="variable">true</span>;
  }

  <span class="keyword">if</span> (<span class="variable">fn</span>)
    <span class="variable">fn</span>(<span class="keyword">null</span>);

  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Helper for console.log</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">EmbeddedDocument</span>.<span class="variable">prototype</span>.<span class="variable">inspect</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="variable">inspect</span>(<span class="this">this</span>.<span class="variable">toObject</span>());
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">EmbeddedDocument</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/index.js"><a href="#">index</a></h2></td><td>lib/types/index.js</td></tr><tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Array</span> = <span class="variable">require</span>(<span class="string">'./array'</span>);
<span class="variable">exports</span>.<span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'./buffer'</span>);

<span class="variable">exports</span>.<span class="class">Document</span> = <span class="comment">// @deprecate</span>
<span class="variable">exports</span>.<span class="class">Embedded</span> = <span class="variable">require</span>(<span class="string">'./embedded'</span>);

<span class="variable">exports</span>.<span class="class">DocumentArray</span> = <span class="variable">require</span>(<span class="string">'./documentarray'</span>);
<span class="variable">exports</span>.<span class="class">Number</span> = <span class="variable">require</span>(<span class="string">'./number'</span>);
<span class="variable">exports</span>.<span class="class">ObjectId</span> = <span class="variable">require</span>(<span class="string">'./objectid'</span>);
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/number.js"><a href="#">number</a></h2></td><td>lib/types/number.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./../utils'</span>)</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherits from Number.
 </p>
</td>
<td class="code">
<pre><code><span class="class">MongooseNumber</span>.<span class="variable">prototype</span> = <span class="keyword">new</span> <span class="class">Number</span>();

<span class="keyword">var</span> <span class="variable">depInc</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseNumber#$inc / MongooseNumber#increment'</span>, <span class="string">'Model.update() to get incrementation in v3'</span>);
<span class="keyword">var</span> <span class="variable">depDec</span> = <span class="variable">utils</span>.<span class="variable">dep</span>(<span class="string">'MongooseNumber#decrement'</span>, <span class="string">'Model.update() to get decrementation in v3'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Atomic increment</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="variable">increment</span> (<span class="variable">value</span>) {
  <span class="variable">depInc</span>();
  <span class="keyword">var</span> <span class="variable">schema</span> = <span class="this">this</span>.<span class="variable">_parent</span>.<span class="variable">schema</span>.<span class="variable">path</span>(<span class="this">this</span>.<span class="variable">_path</span>)
    , <span class="variable">value</span> = <span class="class">Number</span>(<span class="variable">value</span>) || <span class="number integer">1</span>;
  <span class="keyword">if</span> (<span class="variable">isNaN</span>(<span class="variable">value</span>)) <span class="variable">value</span> = <span class="number integer">1</span>;
  <span class="this">this</span>.<span class="variable">_parent</span>.<span class="variable">setValue</span>(<span class="this">this</span>.<span class="variable">_path</span>, <span class="variable">schema</span>.<span class="variable">cast</span>(<span class="this">this</span> + <span class="variable">value</span>));
  <span class="this">this</span>.<span class="variable">_parent</span>.<span class="variable">getValue</span>(<span class="this">this</span>.<span class="variable">_path</span>).<span class="variable">_atomics</span>[<span class="string">'$inc'</span>] = <span class="variable">value</span> || <span class="number integer">1</span>;
  <span class="this">this</span>.<span class="variable">_parent</span>.<span class="variable">_activePaths</span>.<span class="variable">modify</span>(<span class="this">this</span>.<span class="variable">_path</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};
<span class="class">MongooseNumber</span>.<span class="variable">prototype</span>.$<span class="variable">inc</span> =
<span class="class">MongooseNumber</span>.<span class="variable">prototype</span>.<span class="variable">increment</span> = <span class="variable">increment</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns true if we have to perform atomics for this, and no normal
operations</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseNumber</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'doAtomics'</span>, <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">_atomics</span>).<span class="variable">length</span>;
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Atomic decrement</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseNumber</span>.<span class="variable">prototype</span>.<span class="variable">decrement</span> = <span class="keyword">function</span>(){
  <span class="variable">depDec</span>();
  <span class="this">this</span>.<span class="variable">increment</span>(-<span class="number integer">1</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Re-declare toString (for <code>console.log</code>)</p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">MongooseNumber</span>.<span class="variable">prototype</span>.<span class="variable">inspect</span> =
<span class="class">MongooseNumber</span>.<span class="variable">prototype</span>.<span class="variable">toString</span> = <span class="keyword">function</span> () {
  <span class="keyword">return</span> <span class="class">String</span>(<span class="this">this</span>.<span class="variable">valueOf</span>());
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">MongooseNumber</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/types/objectid.js"><a href="#">objectid</a></h2></td><td>lib/types/objectid.js</td></tr><tr class="code">
<td class="docs">
<p>Access driver.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">driver</span> = <span class="variable">global</span>.<span class="class">MONGOOSE_DRIVER_PATH</span> || <span class="string">'../drivers/node-mongodb-native'</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Module exports.
 </p>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">require</span>(<span class="variable">driver</span> + <span class="string">'/objectid'</span>);
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/utils.js"><a href="#">utils</a></h2></td><td>lib/utils.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">EventEmitter</span> = <span class="variable">require</span>(<span class="string">'events'</span>).<span class="class">EventEmitter</span>
  , <span class="class">ObjectId</span> = <span class="variable">require</span>(<span class="string">'./types/objectid'</span>)
  , <span class="class">MongooseBuffer</span>
  , <span class="class">MongooseArray</span>
  , <span class="class">Document</span></code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Pluralization rules.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">rules</span> = [
  [<span class="regexp">/(m)an$/gi</span>, <span class="string">'$1en'</span>],
  [<span class="regexp">/(pe)rson$/gi</span>, <span class="string">'$1ople'</span>],
  [<span class="regexp">/(child)$/gi</span>, <span class="string">'$1ren'</span>],
  [<span class="regexp">/^(ox)$/gi</span>, <span class="string">'$1en'</span>],
  [<span class="regexp">/(ax|test)is$/gi</span>, <span class="string">'$1es'</span>],
  [<span class="regexp">/(octop|vir)us$/gi</span>, <span class="string">'$1i'</span>],
  [<span class="regexp">/(alias|status)$/gi</span>, <span class="string">'$1es'</span>],
  [<span class="regexp">/(bu)s$/gi</span>, <span class="string">'$1ses'</span>],
  [<span class="regexp">/(buffal|tomat|potat)o$/gi</span>, <span class="string">'$1oes'</span>],
  [<span class="regexp">/([ti])um$/gi</span>, <span class="string">'$1a'</span>],
  [<span class="regexp">/sis$/gi</span>, <span class="string">'ses'</span>],
  [<span class="regexp">/(?:([^f])fe|([lr])f)$/gi</span>, <span class="string">'$1$2ves'</span>],
  [<span class="regexp">/(hive)$/gi</span>, <span class="string">'$1s'</span>],
  [<span class="regexp">/([^aeiouy]|qu)y$/gi</span>, <span class="string">'$1ies'</span>],
  [<span class="regexp">/(x|ch|ss|sh)$/gi</span>, <span class="string">'$1es'</span>],
  [<span class="regexp">/(matr|vert|ind)ix|ex$/gi</span>, <span class="string">'$1ices'</span>],
  [<span class="regexp">/([m|l])ouse$/gi</span>, <span class="string">'$1ice'</span>],
  [<span class="regexp">/(quiz)$/gi</span>, <span class="string">'$1zes'</span>],
  [<span class="regexp">/s$/gi</span>, <span class="string">'s'</span>],
  [<span class="regexp">/$/gi</span>, <span class="string">'s'</span>]
];</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Uncountable words.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">uncountables</span> = [
  <span class="string">'advice'</span>,
  <span class="string">'energy'</span>,
  <span class="string">'excretion'</span>,
  <span class="string">'digestion'</span>,
  <span class="string">'cooperation'</span>,
  <span class="string">'health'</span>,
  <span class="string">'justice'</span>,
  <span class="string">'labour'</span>,
  <span class="string">'machinery'</span>,
  <span class="string">'equipment'</span>,
  <span class="string">'information'</span>,
  <span class="string">'pollution'</span>,
  <span class="string">'sewage'</span>,
  <span class="string">'paper'</span>,
  <span class="string">'money'</span>,
  <span class="string">'species'</span>,
  <span class="string">'series'</span>,
  <span class="string">'rain'</span>,
  <span class="string">'rice'</span>,
  <span class="string">'fish'</span>,
  <span class="string">'sheep'</span>,
  <span class="string">'moose'</span>,
  <span class="string">'deer'</span>,
  <span class="string">'news'</span>
];</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Inherit from EventEmitter.
   </p>
</td>
<td class="code">
<pre><code><span class="class">Events</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">EventEmitter</span>.<span class="variable">prototype</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add <code>once</code>.
   </p>
</td>
<td class="code">
<pre><code><span class="class">Events</span>.<span class="variable">prototype</span>.<span class="variable">once</span> = <span class="keyword">function</span> (<span class="variable">type</span>, <span class="variable">listener</span>) {
    <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
    <span class="variable">self</span>.<span class="variable">on</span>(<span class="variable">type</span>, <span class="keyword">function</span> <span class="variable">g</span>(){
      <span class="variable">self</span>.<span class="variable">removeListener</span>(<span class="variable">type</span>, <span class="variable">g</span>);
      <span class="variable">listener</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
    });
  };

}

<span class="variable">exports</span>.<span class="class">EventEmitter</span> = <span class="class">Events</span>;

<span class="comment">// Modified from node/lib/assert.js</span>
<span class="variable">exports</span>.<span class="variable">deepEqual</span> = <span class="keyword">function</span> <span class="variable">deepEqual</span> (<span class="variable">a</span>, <span class="variable">b</span>) {
  <span class="keyword">if</span> (<span class="variable">a</span> === <span class="variable">b</span>) <span class="keyword">return</span> <span class="variable">true</span>;

  <span class="keyword">if</span> (<span class="variable">a</span> <span class="variable">instanceof</span> <span class="class">Date</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">b</span> <span class="variable">instanceof</span> <span class="class">Date</span>)
    <span class="keyword">return</span> <span class="variable">a</span>.<span class="variable">getTime</span>() === <span class="variable">b</span>.<span class="variable">getTime</span>();

  <span class="keyword">if</span> (<span class="variable">a</span> <span class="variable">instanceof</span> <span class="class">ObjectId</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">b</span> <span class="variable">instanceof</span> <span class="class">ObjectId</span>) {
    <span class="keyword">return</span> <span class="variable">a</span>.<span class="variable">toString</span>() === <span class="variable">b</span>.<span class="variable">toString</span>();
  }

  <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">a</span> !== <span class="string">'object'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="keyword">typeof</span> <span class="variable">b</span> !== <span class="string">'object'</span>)
    <span class="keyword">return</span> <span class="variable">a</span> == <span class="variable">b</span>;

  <span class="keyword">if</span> (<span class="variable">a</span> === <span class="keyword">null</span> || <span class="variable">b</span> === <span class="keyword">null</span> || <span class="variable">a</span> === <span class="variable">undefined</span> || <span class="variable">b</span> === <span class="variable">undefined</span>)
    <span class="keyword">return</span> <span class="variable">false</span>

  <span class="keyword">if</span> (<span class="variable">a</span>.<span class="variable">prototype</span> !== <span class="variable">b</span>.<span class="variable">prototype</span>) <span class="keyword">return</span> <span class="variable">false</span>;

  <span class="comment">// Handle MongooseNumbers</span>
  <span class="keyword">if</span> (<span class="variable">a</span> <span class="variable">instanceof</span> <span class="class">Number</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">b</span> <span class="variable">instanceof</span> <span class="class">Number</span>) {
    <span class="keyword">return</span> <span class="variable">a</span>.<span class="variable">valueOf</span>() === <span class="variable">b</span>.<span class="variable">valueOf</span>();
  }

  <span class="keyword">if</span> (<span class="class">Buffer</span>.<span class="variable">isBuffer</span>(<span class="variable">a</span>)) {
    <span class="keyword">if</span> (!<span class="class">Buffer</span>.<span class="variable">isBuffer</span>(<span class="variable">b</span>)) <span class="keyword">return</span> <span class="variable">false</span>;
    <span class="keyword">if</span> (<span class="variable">a</span>.<span class="variable">length</span> !== <span class="variable">b</span>.<span class="variable">length</span>) <span class="keyword">return</span> <span class="variable">false</span>;
    <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">len</span> = <span class="variable">a</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {
      <span class="keyword">if</span> (<span class="variable">a</span>[<span class="variable">i</span>] !== <span class="variable">b</span>[<span class="variable">i</span>]) <span class="keyword">return</span> <span class="variable">false</span>;
    }
    <span class="keyword">return</span> <span class="variable">true</span>;
  }

  <span class="keyword">if</span> (<span class="variable">isMongooseObject</span>(<span class="variable">a</span>)) <span class="variable">a</span> = <span class="variable">a</span>.<span class="variable">toObject</span>();
  <span class="keyword">if</span> (<span class="variable">isMongooseObject</span>(<span class="variable">b</span>)) <span class="variable">b</span> = <span class="variable">b</span>.<span class="variable">toObject</span>();

  <span class="keyword">try</span> {
    <span class="keyword">var</span> <span class="variable">ka</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">a</span>),
        <span class="variable">kb</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">b</span>),
        <span class="variable">key</span>, <span class="variable">i</span>;
  } <span class="keyword">catch</span> (<span class="variable">e</span>) {<span class="comment">//happens when one is a string literal and the other isn't</span>
    <span class="keyword">return</span> <span class="variable">false</span>;
  }

  <span class="comment">// having the same number of owned properties (keys incorporates</span>
  <span class="comment">// hasOwnProperty)</span>
  <span class="keyword">if</span> (<span class="variable">ka</span>.<span class="variable">length</span> != <span class="variable">kb</span>.<span class="variable">length</span>)
    <span class="keyword">return</span> <span class="variable">false</span>;

  <span class="comment">//the same set of keys (although not necessarily the same order),</span>
  <span class="variable">ka</span>.<span class="variable">sort</span>();
  <span class="variable">kb</span>.<span class="variable">sort</span>();

  <span class="comment">//~~~cheap key test</span>
  <span class="keyword">for</span> (<span class="variable">i</span> = <span class="variable">ka</span>.<span class="variable">length</span> - <span class="number integer">1</span>; <span class="variable">i</span> &<span class="variable">gt</span>;= <span class="number integer">0</span>; <span class="variable">i</span>--) {
    <span class="keyword">if</span> (<span class="variable">ka</span>[<span class="variable">i</span>] != <span class="variable">kb</span>[<span class="variable">i</span>])
      <span class="keyword">return</span> <span class="variable">false</span>;
  }

  <span class="comment">//equivalent values for every corresponding key, and</span>
  <span class="comment">//~~~possibly expensive deep test</span>
  <span class="keyword">for</span> (<span class="variable">i</span> = <span class="variable">ka</span>.<span class="variable">length</span> - <span class="number integer">1</span>; <span class="variable">i</span> &<span class="variable">gt</span>;= <span class="number integer">0</span>; <span class="variable">i</span>--) {
    <span class="variable">key</span> = <span class="variable">ka</span>[<span class="variable">i</span>];
    <span class="keyword">if</span> (!<span class="variable">deepEqual</span>(<span class="variable">a</span>[<span class="variable">key</span>], <span class="variable">b</span>[<span class="variable">key</span>])) <span class="keyword">return</span> <span class="variable">false</span>;
  }

  <span class="keyword">return</span> <span class="variable">true</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Merges <code>from</code> into <code>to</code> without overwriting
existing properties of <code>to</code>.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  to</p></li><li><p><strong>param</strong>: <em>Object</em>  from</p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="variable">merge</span> = <span class="keyword">function</span> <span class="variable">merge</span> (<span class="variable">to</span>, <span class="variable">from</span>) {
  <span class="keyword">var</span> <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">from</span>)
    , <span class="variable">i</span> = <span class="variable">keys</span>.<span class="variable">length</span>
    , <span class="variable">key</span>

  <span class="keyword">while</span> (<span class="variable">i</span>--) {
    <span class="variable">key</span> = <span class="variable">keys</span>[<span class="variable">i</span>];
    <span class="keyword">if</span> (<span class="string">'undefined'</span> === <span class="keyword">typeof</span> <span class="variable">to</span>[<span class="variable">key</span>]) {
      <span class="variable">to</span>[<span class="variable">key</span>] = <span class="variable">from</span>[<span class="variable">key</span>];
    } <span class="keyword">else</span> {
      <span class="variable">merge</span>(<span class="variable">to</span>[<span class="variable">key</span>], <span class="variable">from</span>[<span class="variable">key</span>]);
    }
  }
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A faster Array.prototype.slice.call(arguments) alternative
 </p>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="variable">args</span> = <span class="keyword">function</span> (<span class="variable">args</span>, <span class="variable">slice</span>, <span class="variable">sliceEnd</span>) {
  <span class="keyword">var</span> <span class="variable">ret</span> = [];
  <span class="keyword">var</span> <span class="variable">start</span> = <span class="variable">slice</span> || <span class="number integer">0</span>;
  <span class="keyword">var</span> <span class="variable">end</span> = <span class="number integer">3</span> === <span class="variable">arguments</span>.<span class="variable">length</span>
    ? <span class="variable">sliceEnd</span>
    : <span class="variable">args</span>.<span class="variable">length</span>;

  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="variable">start</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">end</span>; ++<span class="variable">i</span>) {
    <span class="variable">ret</span>[<span class="variable">i</span> - <span class="variable">start</span>] = <span class="variable">args</span>[<span class="variable">i</span>];
  }

  <span class="keyword">return</span> <span class="variable">ret</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns if <code>v</code> is a mongoose object that has
a <code>toObject()</code> method we can use. This is for
compatibility with libs like Date.js which do
foolish things to Natives.
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">isMongooseObject</span> = <span class="variable">exports</span>.<span class="variable">isMongooseObject</span> = <span class="keyword">function</span> (<span class="variable">v</span>) {
  <span class="class">Document</span> || (<span class="class">Document</span> = <span class="variable">require</span>(<span class="string">'./document'</span>));
  <span class="class">MongooseArray</span> || (<span class="class">MongooseArray</span> = <span class="variable">require</span>(<span class="string">'./types'</span>).<span class="class">Array</span>);
  <span class="class">MongooseBuffer</span> || (<span class="class">MongooseBuffer</span> = <span class="variable">require</span>(<span class="string">'./types'</span>).<span class="class">Buffer</span>);

  <span class="keyword">return</span> <span class="variable">v</span> <span class="variable">instanceof</span> <span class="class">Document</span> ||
         <span class="variable">v</span> <span class="variable">instanceof</span> <span class="class">MongooseArray</span> ||
         <span class="variable">v</span> <span class="variable">instanceof</span> <span class="class">MongooseBuffer</span>
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>API deprecation helper.</p>

<p>Disable deprecation warnings by setting the
MONGOOSE<em>DEP</em>WARNINGS environment variable to false, or
mongoose.set('dep warnings', false);
 </p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">warn</span> =<span class="variable">false</span>;
;(<span class="variable">exports</span>.<span class="variable">dep</span> = <span class="keyword">function</span> <span class="variable">deprecate</span> (<span class="variable">old</span>, <span class="variable">rec</span>, <span class="variable">fn</span>, <span class="variable">scope</span>) {
  <span class="keyword">return</span> <span class="keyword">function</span> () {
    <span class="keyword">if</span> (<span class="variable">warn</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="variable">exports</span>.<span class="variable">dep</span>.<span class="variable">cache</span>[<span class="variable">old</span>])  {
      <span class="variable">exports</span>.<span class="variable">dep</span>.<span class="variable">cache</span>[<span class="variable">old</span>] = <span class="number integer">1</span>;
      <span class="variable">console</span>.<span class="variable">warn</span>(<span class="string">'*** Mongoose %s is deprecated. Use %s instead.'</span>, <span class="variable">old</span>, <span class="variable">rec</span>);
    }
    <span class="keyword">return</span> <span class="variable">fn</span>
      ? <span class="variable">fn</span>.<span class="variable">apply</span>(<span class="variable">scope</span> || <span class="this">this</span>, <span class="variable">arguments</span>)
      : <span class="variable">undefined</span>
  }
}).<span class="variable">cache</span> = {};

<span class="variable">process</span>.<span class="variable">nextTick</span>(<span class="keyword">function</span> () {
  <span class="keyword">var</span> <span class="variable">mongoose</span> = <span class="variable">require</span>(<span class="string">'./index'</span>);
  <span class="keyword">var</span> <span class="variable">val</span> = <span class="variable">mongoose</span>.<span class="variable">set</span>(<span class="string">'dep warnings'</span>);
  <span class="keyword">if</span> (<span class="string">'boolean'</span> == <span class="keyword">typeof</span> <span class="variable">val</span>) {
    <span class="variable">warn</span> = <span class="variable">val</span>;
  } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'false'</span> !== <span class="variable">process</span>.<span class="variable">env</span>.<span class="class">MONGOOSE_DEP_WARNINGS</span>) {
    <span class="variable">warn</span> = <span class="variable">true</span>;
  }
});
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/virtualtype.js"><a href="#">virtualtype</a></h2></td><td>lib/virtualtype.js</td></tr><tr class="code">
<td class="docs">
<p>VirtualType constructor</p>

<p>This is what mongoose uses to define virtual attributes via
<code>Schema.prototype.virtual</code></p>

<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">function</span> <span class="class">VirtualType</span> (<span class="variable">options</span>) {
  <span class="this">this</span>.<span class="variable">getters</span> = [];
  <span class="this">this</span>.<span class="variable">setters</span> = [];
  <span class="this">this</span>.<span class="variable">options</span> = <span class="variable">options</span> || {};
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a getter</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  fn</p></li><li><p><strong>return</strong>: <em>VirtualType</em>  this</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">VirtualType</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="this">this</span>.<span class="variable">getters</span>.<span class="variable">push</span>(<span class="variable">fn</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Adds a setter</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Function</em>  fn</p></li><li><p><strong>return</strong>: <em>VirtualType</em>  this</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">VirtualType</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">fn</span>) {
  <span class="this">this</span>.<span class="variable">setters</span>.<span class="variable">push</span>(<span class="variable">fn</span>);
  <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Applies getters</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  value</p></li><li><p><strong>param</strong>: <em>Object</em>  scope</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">VirtualType</span>.<span class="variable">prototype</span>.<span class="variable">applyGetters</span> = <span class="keyword">function</span> (<span class="variable">value</span>, <span class="variable">scope</span>) {
  <span class="keyword">var</span> <span class="variable">v</span> = <span class="variable">value</span>;
  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">l</span> = <span class="this">this</span>.<span class="variable">getters</span>.<span class="variable">length</span> - <span class="number integer">1</span>; <span class="variable">l</span> &<span class="variable">gt</span>;= <span class="number integer">0</span>; <span class="variable">l</span>--){
    <span class="variable">v</span> = <span class="this">this</span>.<span class="variable">getters</span>[<span class="variable">l</span>].<span class="variable">call</span>(<span class="variable">scope</span>, <span class="variable">v</span>);
  }
  <span class="keyword">return</span> <span class="variable">v</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Applies setters</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Object</em>  value</p></li><li><p><strong>param</strong>: <em>Object</em>  scope</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">VirtualType</span>.<span class="variable">prototype</span>.<span class="variable">applySetters</span> = <span class="keyword">function</span> (<span class="variable">value</span>, <span class="variable">scope</span>) {
  <span class="keyword">var</span> <span class="variable">v</span> = <span class="variable">value</span>;
  <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">l</span> = <span class="this">this</span>.<span class="variable">setters</span>.<span class="variable">length</span> - <span class="number integer">1</span>; <span class="variable">l</span> &<span class="variable">gt</span>;= <span class="number integer">0</span>; <span class="variable">l</span>--){
    <span class="this">this</span>.<span class="variable">setters</span>[<span class="variable">l</span>].<span class="variable">call</span>(<span class="variable">scope</span>, <span class="variable">v</span>);
  }
  <span class="keyword">return</span> <span class="variable">v</span>;
};

<span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">VirtualType</span>;
</code></pre>
</td>
</tr>	</body>
</html></tbody></table>
